Search code examples
matlabrandomnumberslimit

How to generate all numbers randomly in the limit [m,n] in matlab?


How to generate all numbers randomly in the limit [m,n]. To generate all numbers from 6 to 12.. ie., the sequence must be like [7 12 11 9 8 10 6].

r = randi([6 12],1,7);

But this gives the result:

[12  11  12   7   9  10  12] 

Here the numbers are repeated and the sequence does not contain all numbers from 6 to 12.


Solution

  • You can use randperm to make a list of random numbers between 1 and n (where n is the length of your vector), and use that to permute the vector.

    v=6:12;
    n=length(v);
    I=randperm(n);
    v(I)