1

rand() does not seem to generate really random numbers. I have a simple program that returns a 6-digit number by calling :

for i=1:6
 r=rand(1,1)
end 

so I ran this 4-5 times yesterday. And saved the output. Today I opened MATLAB again and called the same function again 4-5 times. The same numbers were returned.

Why is this happening?

Should I provide a random seed or any other fix?

Thanks for any help!

jeff
  • 13,055
  • 29
  • 78
  • 136
  • Even if the answers are really good, I still wonder about one thing. When I run matlab, the numbers generated by `rand` is always different. It seems somehow that there is something that we do not know here. Maybe a seed is already set? Anyway, the answers explains most and this should solve your problems. – patrik Feb 14 '15 at 12:27
  • My case is I ran `rand` a few times, then closed MATLAB, also the computer, and I re-opened everything. Maybe that's what you need to do to "reset" the pseudo-random number generator, therefore to reproduce this behaviour. – jeff Feb 14 '15 at 14:11

2 Answers2

5

To expand on @alexforrence's answer, rand and other related functions produce pseudo-random numbers (PRNs) that require an initial value to begin production. These numbers are not truly random since, following the initial seed, the numbers are produced via an algorithm, which is deterministic by its very nature.

However, being pseudo-random isn't necessarily a bad thing since models that use PRNs (e.g., Monte Carlo Methods) can generate portable, repeatable results across many users and platforms. Additionally, the seed can be changed to create sets of random numbers and results that are statistically independent but also produce repeatable results. For many scientific applications, this is very important. Also, "true" random numbers (next paragraph) have a tendency to "clump" together and not evenly spread over their range for a small sampling of the space, which will degrade the performance of some methods that rely on stochastic processes.

There are methods to create "true-er" random numbers by the introduction of randomness from various analogue sources (e.g., hardware noise). These types of numbers are extremely important for cryptographically secure PRNs, where non-repeatability is an important feature (in contrast to the scientific usage). True random number generators require special hardware that leverages natural noise (e.g., quantum effects). Although, it is important to remember that the total number of random numbers that can be generated and computationally used is limited by the precision of the numbers being used.

You can re-seed MATLAB with a pseudo-random seed using the rng function. However, "reseeding the generator too frequently within a session is not a good idea because the statistical properties of your random numbers can be adversely affected" [src].

TroyHaskin
  • 8,361
  • 3
  • 22
  • 22
  • Nice. +1. BTW, I wrote this answer and gave you a small tip of the hat :) http://stackoverflow.com/questions/28544662/adding-additional-ones-that-surround-other-values-of-one-in-a-vector-in-matlab/28553156#28553156 – rayryeng Feb 17 '15 at 15:34
4

From the Mathworks documentation, you can use

rng('shuffle');

before calling rand to set a "random" seed (based on the current time). Setting the seed manually (either by not changing the seed at startup, by resetting using rng('default'), or setting the seed manually by rng(number)) allows you to exactly repeat previous behavior.

alexforrence
  • 2,724
  • 2
  • 27
  • 30
  • Thanks! I also want to know why this happened. Any ideas? **edit :** I'm confused. Why don't they put `rng('shuffle')` inside `rand()`, then? Because it looks like rand() is not as random as possible. :D – jeff Feb 14 '15 at 00:30
  • MATLAB's random number generator returns to the same state on startup (which you can also get from `rng('default')`, if you want to reproduce behavior). – alexforrence Feb 14 '15 at 00:33
  • I wish it was random and we could save it in an array if we wanted to. Because this way the numbers are actually coming from a pre-determined set. So I still don't get it :D But your answer solves my problem, so I will accept it in a few minutes. – jeff Feb 14 '15 at 00:36
  • I would guess that using `rng('shuffle')` gives you pretty solid random behavior. True randomness is pretty [hard](http://stackoverflow.com/questions/37702/true-random-number-generator). – alexforrence Feb 14 '15 at 00:42
  • 5
    @halilpazarlama: The entire point of *pseudo*random numbers is that they are repeatable. This is essential for scientific purposes where one wants a stream of variates with a give statistical distribution that can be regenerated later if needed. You should always explicitly set your seed in your code via `rng` unless you're writing some sort or encryption routine or a game maybe (neither are Matab's forté and the [default generator](http://en.wikipedia.org/wiki/Mersenne_twister) is not [cryptographically secure](http://en.wikipedia.org/wiki/CSPRNG)). – horchler Feb 14 '15 at 01:53
  • @horchler so **this** is whay they are called pseudo-random? Ok then, seeding it with a timer or this hopefully "trustable" `rng('shuffle')` seems random enough for me :) please let me know if there is a way to get even more random :D – jeff Feb 14 '15 at 14:09
  • @halilpazarlama - Another reason why you would want pseudorandom numbers to be repeatable is for reproducibility in scientific publications. For other reviewers to verify your findings and perhaps to extend your work if you are dealing with randomness, this is required. Not providing your dataset and attributing it to being random would force reviewers and researchers to take your word for it, which is dangerous. You could provide the dataset, but if you are working with large sets of data, that would be infeasible. Therefore, providing the random seed and the environment is more suitable. – rayryeng Feb 17 '15 at 15:38