r/AskComputerScience • u/Forsaken-Job-7126 • Jul 01 '26
Since computer can only generate pseudo random numbers
can't you do really cool trick with the fact that if you were to roll a dice 5 times and all the previous rolls were the number 3 normally with true random ness the 6th roll only gets a 1/6 chance of rolling any number. Is that still true with pseudo randomness ? and if so can you prove it
0
Upvotes
12
u/ghjm MSCS, CS Pro (20+) Jul 01 '26
With a PRNG and a given seed, the actual probability is 100% that it will produce the next number in the sequence, and there is a 0% chance of it doing anything else. But with a good cryptographic PRNG algorithm, if you don't know the seed and only have a list of previous outputs, there is no statistical analysis you can do that would yield any more insight than you would get from actual randomness.
3
u/nuclear_splines Ph.D Data Science Jul 01 '26
For a good PRNG the sixth roll should be conditionally independent from the first five rolls. You can't prove this in general, because you'd need to prove it for each random number generator. Here's a PRNG where that's not true:
But you can demonstrate that your PRNG works much better. Let's reproduce your test in Python using Numpy's PCG-64 pseudo-random number generator:
Here we keep rolling dice (a random integer between 1 and 6 inclusively) until we get five threes in a row. Then we count what the sixth roll is. After we've done that a thousand times, plot the results as a bar plot. It should look like a uniform distribution.
I prefer a visual explanation, but we can get mathier about it. For a fair die we expect our rolls to be uniformly distributed, so for a thousand trials as above, we can expect about 166 rolls per die-face. This more or less matches what I see on the bar plot, but there's a little variation due to chance. Is there too much variation? We can use the Chi-Squared goodness of fit test to check:
Here, the null hypothesis is that our dice rolls come from a uniform distribution. A p-value under 0.05 indicates that there's only a one-in-twenty chance that a real uniform distribution could have yielded these rolls. The higher the number of trials, the more confident we can be in our hypothesis test.
17
u/hibikir_40k Jul 01 '26
On top of the other answers, which are correct regarding the math: Many computers have hardware modules for true random production. They can't necessarily produce them super quickly, but if you want truly random numbers and you are fine with some slowness, you can get them
1
u/JeLuF Jul 01 '26
Most pseudo random generators are pretty good. You need a large number of rolls to notice that there are small deviations from true randomness. So, for example, over millions of rolls, you might see that the sequence '3 4 2 6' is a little bit more likely than it should.
Many computer actually have real random number generators. Modern Intel, AMD or Apple Silicon CPUs all have instructions to get real random numbers, using on-chip physical noise sources.
1
u/Any_Click1257 Jul 01 '26 edited Jul 01 '26
I feel like it's worth point out that your construction is incorrect. Given a fair die, the probability of any single role outcome is 1/6.
The probability of any sequence is the product of the iid probabilities of any one roll, so the probability of 222225 is the same as 222222. Go given 22222, the probability of 2 or 5 on the next roll is 1/6. The previous values have no impact on the next value.
Computers generate random numbers by sampling high entropy inputs, like the mouse position, or certain pixels in the Display RAM
1
u/ThrowAway24Okt Jul 10 '26
Pseudo random number generators have an internal state there and if you know the previous results (5 times in a row it returned 3), you can narrow down what this state could be. this is only useful if you can accurately read random value and if you have enough data to calculate the state if the generator.
People have done this to cheat in for minecraft:
Different events takes two random values from the PRNG, for example the random velocity of a item thrown item. This could be measured and used to determine the internal state of the PRNG. Then they could use that to always get the beneficial effects for some events by consuming the "bad" random numbers for things that didn't matter (velocity of the items). https://www.youtube.com/watch?v=FPmQ0rnJjNc
1
u/Vert354 Jul 01 '26
The issue with pseudo random is that if you know what the seed value is you can determine exactly what the 6th result will be. Statistically though, pseudo random numbers ARE random. If you're just being fed the numbers and don't know anything about how they are being generated they appear random.
1
u/T_Thriller_T Jul 01 '26
To add additional information:
The flat answer to your questions for many Pseudorandom number generator should be yes.
PRNG works with functions. And it is mathematically provable what the distribution of results for a function looks like. Which means we can, quite easily, show that for the function we use to create seemingly random numbers at or after certain sample sizes the distribution is uniform - meaning any possible number is generated more or less as often as any other.
There is an option the a PNRG is even more uniform than actual random numbers. Random numbers are random. While every outcome is as likely as another, this only really works itself out in rather big sample sets. Which is why you can be unlucky enough to roll a 1 ten or even a dozen times in a row.
Or, an even better example: rain is very random. It is very much not uniform. While, after a while, every spot of the ground will be covered, especially in the beginning you will see clusters and areas which seem to be untouched.
1
u/teraflop Jul 01 '26
Even if a computer is totally deterministic, you can still talk about the behavior of a PRNG in terms of its seed.
If the computer did have a true random number generator, then after rolling a bunch of 3's, the outcome of the next dice roll would still be an equal 1/6 probability for each possible outcome.
Assume instead that the dice are rolled using a "good" PRNG, with some unknown seed that was chosen at the start of the program. Say there were N possible seeds. Each particular seed gives a deterministic result for the entire program.
But of those N seeds, only some fraction of them will cause the first 5 rolls to be all 3's. If the PRNG is "good", then this fraction should be roughly 1/65, which is equal to the probability of that event happening with true random numbers. And likewise, if we take that subset leading to a particular outcome for the first 5 rolls, and look within that subset at the distribution of possible outcomes for the 6th roll, those should also be equally likely. So as long as the seed is unpredictable, the output "looks random".
What do we mean by a "good" PRNG? Well, one simple answer is that there shouldn't be noticeable correlations between subsequent outcomes. That is, it's not enough to say that the first roll should give all 6 values with equal probability, and the same for the second roll. It should also be that they are uncorrelated: all 6x6 combinations should be roughly equally probable. And this lack of correlation should hold no matter how many successive values we look at, to within the limits of statistical accuracy. This is something we can try to ensure by the choosing a PRNG with the right mathematical properties, and we can experimentally test it.
But more broadly, the ideal PRNG would be one that is cannot be efficiently distinguished from randomness in any way. That is, every polynomial-time computation on a sequence of PRNG outputs should give results that follow the same distribution as if the outputs were true random numbers. And determining whether or not such an ideal PRNG exists is a very hard problem -- it's at least as difficult as solving the P vs. NP problem. So there is no proof known yet.
1
u/Aggressive_Ad_5454 Jul 02 '26
Computers these days have real random number generators. /dev/rnd on UNIX-alikes. I think they use sources like power noise.
1
u/schungx Jul 02 '26
Computers are deterministic. And they have bounded memory.
That means they are deterministic machines and cannot produce TRUE randomness unless using external sources.
All pseudorandom algorithms compete to get as close as possible to true randomness.