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

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

. To generate all numbers from 6 to 12

.. i.e. the sequence should 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

.

+3


source to share


2 answers


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)

      

+6


source


Assuming you are sampling using a uniform distribution.

r = datasample(6:12,7,'Replace',false)



In a nutshell, this is a random sample with no replacement, so you get all the values ​​from your original population in no particular order.

+3


source







All Articles