News for hackers. Stuff that matters.
Re:How Do Computers Generate Random Numbers? by lsc36 on Monday September 14, 2020 @04:22AM (#24467978)
1. You don't turn PRNG into "true" RNGs simply by picking seeds from environmental randomness. The seed is just the initial state, as long as the output is generated by a deterministic algorithm, by definition it's a PRNG. At the very best you can make a CSPRNG, but not a "true" RNG.

2. The dice roll example is not uniform distribution, I think this is a common pitfall when generating random integers of a range. `randomNumber % 6` results in a slight bias towards 0 and 1, since 2^31 % 6 == 2, there are more numbers in the range [0, 2^31-1] that map to 0 and 1 than those that map to 2...5. To make it uniform, for example, you should always discard if `randomNumber < 2` and regenerate another number for use.

Re:How Do Computers Generate Random Numbers? by ficklepickle on Monday September 14, 2020 @04:58AM (#24468171)
#2 reminds me of Benford's Law, which I recently learned about and find truly fascinating.

https://en.wikipedia.org/wiki/Benford%27s_law

Re:How Do Computers Generate Random Numbers? by chriselles on Monday September 14, 2020 @03:33PM (#24474156)
Interesting.

On first pass, Benford’s Law looks a lot like Zipf’s Law.

What differentiates Benford’s Law from Zipf’s Law?

Re:How Do Computers Generate Random Numbers? by pmiller2 on Monday September 14, 2020 @09:09PM (#24476897)
From https://en.wikipedia.org/wiki/Zipf%27s_law :

> It has been argued that Benford's law is a special bounded case of Zipf's law,[22] with the connection between these two laws being explained by their both originating from scale invariant functional relations from statistical physics and critical phenomena.[24] The ratios of probabilities in Benford's law are not constant. The leading digits of data satisfying Zipf's law with s = 1 satisfy Benford's law.

Re:How Do Computers Generate Random Numbers? by chriselles on Monday September 14, 2020 @11:42PM (#24477679)
Thank you!

I’ll take some time to try and better understand your post.

Re:How Do Computers Generate Random Numbers? by blauditore on Monday September 14, 2020 @07:39AM (#24468995)
Re 2.: OTOH, the bias is extremely tiny, there's only 2 out of 2^31 cases which are problematic, or one in a billion. A generic RNG obviously shouldn't do it this way, but for most applications it would be good-enough really.
Re:How Do Computers Generate Random Numbers? by lsc36 on Monday September 14, 2020 @08:00AM (#24469094)
The article claims that "it is indeed a uniform distribution" so I wanted to point it out.
Re:How Do Computers Generate Random Numbers? by teddyh on Monday September 14, 2020 @07:42AM (#24469005)
To make a dice roll uniform, wouldn’t it be easier to do randomNumber % 8 and only use it if it is from 0 to 5, and discard and re-roll otherwise?

(Since we can reasonably assume that randomNumber is a binary number, and thus would be balanced over 8 values instead of 6.)

Re:How Do Computers Generate Random Numbers? by lsc36 on Monday September 14, 2020 @08:02AM (#24469107)
That depends on the modulus being divisible by 8, which is not always the case. A common example is modulus = 2^31 - 1 [0] which is prime.

[0] https://en.wikipedia.org/wiki/Linear_congruential_generator#...

Re:How Do Computers Generate Random Numbers? by atg_abhishek on Monday September 14, 2020 @04:31AM (#24468031)
Ah very true, an often ignored thing, thank you for sharing!