In a loop it's enough to set the seed one time to get a sequence of random numbers. How does the function rand generate a lot of random numbers based on one seed only ?
-
related: [How does rand() work? Does it have certain tendencies? Is there something better to use?](http://stackoverflow.com/questions/3539398/how-does-rand-work-does-it-have-certain-tendencies-is-there-something-better) – user4581301 Feb 17 '17 at 22:51
-
1How does Fibonacci generate a lots of numbers starting from just 1? There's a simple rule that leads from one element in the succession to the next one; and in the case of `rand` it is such that the numbers *look* quite random (both to the casual observer and according to some basic statistical tests). – Matteo Italia Feb 17 '17 at 22:52
-
you always get a sequence from rngs and actually it is an important feature of rngs that they always produce the same sequence given the same seed. After all they arent that random – 463035818_is_not_an_ai Feb 17 '17 at 22:53
-
https://en.wikipedia.org/wiki/Random_number_generation – Barmar Feb 17 '17 at 22:53
2 Answers
All you need is an algorithm to turn one number into two numbers. One number becomes your next output, the other becomes your next seed. Such algorithms are quite simple. For example, you could multiply the number by two different primes and then reduce each mod 65536. That would produce two outputs from a single input.
An alternative method is even simpler:
1) Permute the seed by multiplying it by one number and adding a second number. (For example, seed = seed * 214013 + 2531011;
.)
2) Output only part of the new seed. (For example, return (seed >> 16) &0x7ffff;
.)

- 179,497
- 17
- 214
- 278
It is actually Pseudorandom, the numbers appear random but are not, they are deterministic since they can be predicted by the algorithm. A truly random number cannot be predicted. Using the same seed will produce the same sequence. The standard specifies and algorithm, but the actual algorithm used will depend on the implementation.

- 5,437
- 28
- 44