With programming, it is never "random". Even the random generator uses an algorithm to predict a random number. But, if knowing the method of generation, is it possible to, let's say predict next 5 numbers that will be generated?
-
You can just run the number generator again. That will give you the next number. – Mysticial Jun 28 '13 at 19:18
-
So, if I'm trying to use the same generator on the same range of numbers, I will always get the same result? How is it different from making a static array of numbers, then? – Jun 28 '13 at 19:19
-
Yes, with difficulty varying greatly on the RNG algorithm used. – Kevin Jun 28 '13 at 19:20
-
@Scripty: It's not really any different than having a static array of predetermined random numbers, except the algorithm takes like 100 bytes, and the static array for `rand()` in GCC would take ~16GB. – Mooing Duck Jun 28 '13 at 19:22
-
Nowdays most people use a Mersenne twister, which would take ~2*10^19935 bytes. For a sense of scale, there are approx ~2*10^82 atoms in the obvervable universe. – Mooing Duck Jun 28 '13 at 19:28
-
If you use a pseudo-random number generator and "seed" it with the same seed, it will produce the same sequence every time. This is often useful for testing, etc. – Hot Licks Jun 28 '13 at 19:29
-
@Scripty A typical prng have a very large period, so you'd have to use an enormous array to emulate it. (so large it is not physically possible) The principle would be the same though. One more thing you need is the seed for the prng, this would be the starting point, in this analogy it would the index in the array you start at. – nos Jun 28 '13 at 19:29
-
@mooingDuck Why 2*10^19935 bytes? Shouldn't it be the vastly smaller (but still astronomically large) 2^19937 - 1 *bits* (the period of the algorithm)? – JBentley Feb 21 '14 at 19:21
-
@JBentley: Er, yes. That. – Mooing Duck Feb 21 '14 at 19:55
3 Answers
Yes, it is possible to predict what number a random number generator will produce next. I've seen this called cracking, breaking, or attacking the RNG. Searching for any of those terms along with "random number generator" should turn up a lot of results.
Read How We Learned to Cheat at Online Poker: A Study in Software Security for an excellent first-hand account of how a random number generator can be attacked. To summarize, the authors figured out what RNG was being used based on a faulty shuffling algorithm employed by an online poker site. They then figured out the RNG seed by sampling hands that were dealt. Once they had the algorithm and the seed, they knew exactly how the deck would be arranged after later shuffles.

- 5,339
- 2
- 48
- 48

- 398,270
- 210
- 566
- 880
-
I just tried a random.nextInt(10); twice in Java, and got different results... – Jun 28 '13 at 20:18
-
9@Scripty Look at the documentation for [Random](http://docs.oracle.com/javase/7/docs/api/java/util/Random.html#nextInt%28int%29). 10 is the range in that statement, not the seed. Create two `Random` objects with the same seed and they'll give you the same sequence of random numbers. – Bill the Lizard Jun 28 '13 at 20:26
Assuming a deterministic algorithhm. Create two identical random number generators. Ask the first one what the second one will produce next -- 5 times.

- 9,166
- 3
- 34
- 52
The vast majority of "random number generators" are really "pseudo-random number generators", which means that, given the same starting point (seed) they will reproduce the same sequence. In theory, by observing the sequence of numbers over a period of time (and knowing the particular algorithm) one can predict the next number, very much like "cracking" an encryption.
The time/effort required to do this will vary greatly depending on the specific algorithm, of course. RNGs that are "cryptographic" will be very much harder to predict than your garden-variety RNG. But for most uses of random numbers this sort of predictability is not a problem.

- 47,103
- 17
- 93
- 151