Click to See Complete Forum and Search --> : How To Generate a Set of Random Numbers From a Fixed Set of Numbers?


Hsiangsta
April 9th, 2008, 01:36 AM
Sup everyone!

I am trying to generate a set of random numbers B from a fixed set A = [1 2 3 4 5 6 7 8 9] in MATLAB with no repeats. So for example,

B = [3 2 4 7 1 8 9 6 5]

is one possibility. The trouble I am having is that I am not sure how to check that there are no repeats in set B.

Does anyone know how to solve this? Thanks a lot!

~Hsiang

kumaresh_ana
April 9th, 2008, 05:35 AM
This is called shuffling (http://en.wikipedia.org/wiki/Shuffling).

Hsiangsta
April 9th, 2008, 11:55 AM
Hey thanks a lot! I managed to find that the MATLAB function "randperm" does the shuffling and can be implemented as so:

y = x(randperm(length(x)));

where x is the array you wish to shuffle. Hope this may help others having the same problem as well!