As a bonus, this week I was curious how long it would take me to find a random number seed that would produce a certain set of numbers from a pool of fifty (essentially playing the lottery in MATLAB). I was pleased to see it took only three minutes to find the seed that would generate my six numbers. It was a cool exercise in Distributed Computing to be able to test every random number seed until I found the one I was looking for.
rand('state',5967887)
sort(ceil(rand(1,6) * 50))
ans =
4 8 15 16 23 42
22:00 UTC |
Posted in Uncategorized |
Permalink |
You can follow any responses to this entry through the RSS 2.0 feed.
You can skip to the end and leave a response. Pinging is currently not allowed.
Leave a Reply
Hi Doug,
I saw this post because I was looking for a similar function that would generate random numbers and at the same time whole numbers. I am a newbie in Matlab and I checked out your statement and ran through a number of iterations and on the sixth iteration there are two numbers that are the same. This will be unlikely for lottery picks because it can only pick the numbers that are still remaining not the ones that have already been picked. I would also like to ask what does the whole number after ’state’ do for the rand function.
Rommel,
You are correct; this is not a true “lottery” simulation. It is essentially generating a vector of six uniformly distributed numbers ([0 1]) and then scaling them to [0 50] and finally rounding them up to be only integers ([1 50]). The essentially means there is ‘replacement’ between picks. It would be easy enough to use the “unique” function in MATLAB to ensure you had unique values, or you could use “randperm” to get a random permutation of the numbers and just select the first few.
The number after ’state’ is setting up the state of the random number generator. This is much like setting the seed. I did that because I was specifically looking to generate “4 8 15 16 23 42″ as my lottery picks.
Thanks for the interest,
Doug