Hacker News new | ask | show | jobs
by Semaphor 910 days ago
I once wrote a small script (in C#) to pick a few tens out of a few 1000s. I got weird repeats. I switched to a cryptographically secure RNG, and the repeats were gone. It was probably pure chance, but I stopped using the normal random function ever since ;)
2 comments

Just for curiosity sake, I looked at the standard RNG in C# and it is indeed flawed, but probably not enough to be seen unless you are doing formal tests.

In [1] it says that numbers have a 0.5034 probability of being odd due to rounding errors in the algorithm that picks a number in a range, which is unacceptable for a simulation, but may actually be better than a real coin toss [2]. The raw RNG is also flawed but not that badly [3]

[1] https://fuglede.dk/en/blog/bias-in-net-rng/

[2] https://arxiv.org/abs/2310.04153

[3] https://gist.github.com/fuglede/772402ecc3997ada82a03ce65361...

Okay, now you made me want actually look into that ;) I should be able to recreate the old code (it was years ago, but I still have the script) and make a comment if I end up actually finding statistical anomalies.
It will have to stay a mystery, looking at distribution, both algorithms looked the same.
Do you still have your original code? Finding out if the weird repeats were real, and especially why, would make a super interesting blog post.
The old code was var r = new Random() ;)

I'm assuming I either hit an PRNG bug (after all, I encountered a compiler bug in university already), or more likely, that I simply imagined the issue or bad bad luck with the data. Never did a statistical analysis after all.