Say I want 5 numbers between 1 to 10. However, I do not want any number to be repeated. How do I do this?
I thought of doing
randi([1,length(a)])
Or this :
(10-1).*rand(5,1) + 1
But then, this only gives me one number at a time! I want unique numbers and this will nto guarantee it.
One way to do it is by using
randperm
:This can be generalized to any set of values:
The problem is when N is large and n is small:
Then you need to find a better solution. If you have the Statistics Toolbox, try:
Another approach, which is also slow but doesn't make use of
randperm
orrandsample
: