Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does stdlib's rand() always give the same sequence?

Tags:

c

random

std

I quite like being able to generate the same set of pseudo-random data repeatedly, especially with tweaking experimental code. Through observation I would say that rand() seems to give the same sequence of numbers each time*.

Is it guaranteed to do this for repeated executions on the same machine / for different machines / for different architectures?

*For the same seed obviously.

like image 549
Joe Avatar asked Aug 17 '10 14:08

Joe


People also ask

Why does rand () give the same number?

The RAND function in stand-alone applications generates the same numbers each time you run your application because the uniform random number generator that RAND uses is initialized to same state when the application is loaded.

How does one prevent the random number generator from always producing the same sequence?

One simple way to avoid repeating the same random numbers in a new MATLAB session is to choose a different seed for the random number generator. rng gives you an easy way to do that, by creating a seed based on the current time. Each time you use 'shuffle' , it reseeds the generator with a different seed.

Is Rand function truly random?

Description. RAND returns an evenly distributed random real number greater than or equal to 0 and less than 1. A new random real number is returned every time the worksheet is calculated. Note: As of Excel 2010, Excel uses the Mersenne Twister algorithm (MT19937) to generate random numbers.

Why Rand gives the same numbers C++?

Every time you call rand() it takes the seed and/or the last random number(s) generated (the C standard doesn't specify the algorithm used, though C++11 has facilities for specifying some popular algorithms), runs a mathematical operation on those numbers, and returns the result.


2 Answers

Yes, given the same environment for the program. From the C standard §7.20.2.2/2,

The srand function uses the argument as a seed for a new sequence of pseudo-random numbers to be returned by subsequent calls to rand. If srand is then called with the same seed value, the sequence of pseudo-random numbers shall be repeated. If rand is called before any calls to srand have been made, the same sequence shall be generated as when srand is first called with a seed value of 1.

Of course, this assumes it is using the same implementation detail (i.e. same machine, same library at the same execution period). The C standard does not mandate a standard random number generating algorithm, thus, if you run the program with a different C standard library, one may get a different random number sequence.

See the question Consistent pseudo-random numbers across platforms if you need a portable and guaranteed random number sequence with a given seed.

like image 123
kennytm Avatar answered Oct 16 '22 23:10

kennytm


It is guaranteed to give the same sequence for the same seed passed to srand() - but only for the duration of a single execution of the program. In general, if an implementation has a choice in behaviour, there is no specific requirement for that choice to remain the same across subsequent executions.

It would be conforming for an implementation to pick a "master seed" at each program startup, and use that to perturb the pseudo-random number generator in a way that is different each time the program starts.

If you wish for more determinism, you should implement a PRNG with specific parameters in your program.

like image 34
caf Avatar answered Oct 17 '22 00:10

caf