How to use randperm command with same starting and ending number each time to generate random numbers?

1 view (last 30 days)
N=7;
Startingnumber= any number from 1 to N
EndingNumber=any number from 1 to N
I need to create R=randperm(N-2) and the logic is that starting and ending number must not be included. So, I can obtain;
X=[Startingnumber R Endingnumber]
Example:
N=7;
Startingnumber= 3;
EndingNumber=6;
R=2 1 5 7 4 (randomly generated)
output;
X=[3 2 1 5 7 4 6]
OR, if there is any other command?
______________________________________________________________________________________________________
Thankyou!

Accepted Answer

DGM
DGM on 1 Oct 2021
Edited: DGM on 1 Oct 2021
Here's one way. It can probably be simpler than this, but eh.
N = 7;
startnum = 3;
endnum = 6;
R = 1:N;
R = R((R~=startnum) & (R~=endnum));
R = [startnum R(randperm(N-2)) endnum]
R = 1×7
3 7 4 2 1 5 6

More Answers (0)

Categories

Find more on Random Number Generation in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!