Ooh I love talking about this!
Perl is generating the random number. Actually Linux really is and Perl is handling the call and giving the result, returning a number between 0 and 1 (0 inclusive, 1 not). This number is to 15 decimal places. Or, thinking about it another way, a number between 1 and 1,000,000,000,000,000. I had to look that up, it's 1 quadrillion.
Extrapolating on the 1-256 example - you are right, 6 (again, using the example) doesn't round into a quadrillion nicely, it'll be biased.
Same math - 166,666,666,666,666 x 6 = 999,999,999,999,996. Ergo the last 4 will produced biased data. Or about 0.0000000000004%.
Technically it's not the "last" 4 either. With every random generator rounding or modulus has to happen somewhere along the way, so you'll get a distribution of rolls which is something like:
- 1: 166,666,666,666,667 (+0.0000000000006%)
- 2: 166,666,666,666,667 (+0.0000000000006%)
- 3: 166,666,666,666,666
- 4: 166,666,666,666,667 (+0.0000000000006%)
- 5: 166,666,666,666,667 (+0.0000000000006%)
- 6: 166,666,666,666,666
Btw 8, 10, 20, 100 don't have this issue as they round nicely.
Rathmun:
The problem with this sort of thinking is that it's still suceptable to streaky RNG.
If you have a coin that flips heads 100x in a row, followed by tails 100x in a row, and just repeats that pattern forever, I'd hardly call it a fair coin. But if you flip it a million times, you'll still end up with 500000 heads and 500000 tails.
That's why
the analyser also checks duplicate rolls (as well as walking rolls and deviations). It's not just a pretty grid.
I do recall many moons ago one of the many complaints about the roller. In this particular instance there were complaints it was rolling low all the time, which is not surprising as it's a recurring theme. The story that I always tell at this point is I changed the random generator from "rand()" to "1 - rand()", which flips the results on their head. The next complaint was.. how the dice roller is always low.
In the past we've used base rand(), 1 - rand(), Math::Random, modulus and even a random generator to randomly choose the random generator.. but they've all resulted in the same complaints.
I've generated 2 million rolls and run it through
ent and it's shown no issues outside of the pseudo-random norms. I took considerable time to create the dice analyser and since its introduction the dice roller has generated and tracked over 8 million rolls, of which the results have by and large been dismissed.
I've flipped and changed how we generate numbers and none of it has appeased anyone. I've analysed results, and provided said analysis, but it still doesn't help. I gave up messing with it some time ago and just resorted to Perl's well-documents and well-tested rand().
I have been disappointed in my own rolls here but I've come to realised that us humans are far more biased than the dice roller. For that matter so are the physical dice we use, check out
this where they compare 10,000 Perl rolls vs real dice… and Perl is far more consistent (TLDR - Perl's largest skew is +7.4%, readl dice - 41.0%).
I love a good conspiracy theory and are happy to make improvements as required but at this point there's nothing new to make me doubt that Linux's and Perl's random number generation is not good enough for our needs. It's been widely audited and is considered more than adequate for anything short of cryptography.