75 points · by lowleveldesign · 8y ago · 40 comments
https://github.com/dotnet/corefx/issues/23298
I wrote some replacement classes that address all of the known issues, here:
https://github.com/colgreen/Redzen/tree/master/Redzen/Random
I think so, too. Because it was not only referred to, but also linked ;-)
- It is not thread-safe (and might start outputting a serie of 0 when called in parallel)
- There is a bug (acknowledged by Microsoft but not fixed for backward compatibility reasons) in the implementation meaning that the generator has an abnormally short period and is, overall, less random looking.
There's enough scenarios like this that making a change to a PRNG algorithm is a very dangerous and breaking change. People rely on the fact that, given the same seed, you get the same sequence of values.
I understand not wanting to change the implementation now, but users should never have assumed it would be stable in the first place.
Once upon a time that was inconvenient for me. I found Math.NET Numerics to be a good replacement.
Also check out threadlocal for thread-safety.
[0] https://docs.microsoft.com/en-us/dotnet/api/system.random
https://stackoverflow.com/questions/19270507/correct-way-to-...
"RNGCryptoServiceProvider is generally a safer choice when you need to generate random bytes. Creating an instance of this class is expensive, so it’s better to populate a 400-byte array than call the constructor 100 times to populate a 4-byte array."
Why would you call the constructor 100 times to popuate 100 4-byte arrays?
It's a service, surely the best approach to call the constructor once, then populate 100 4-byte arrays by calling GetBytes 100 times from the same service?
It is explicit code without the downside of the expensive constructor.
edit for clarification: It's true that getting all the data at once will still be much faster because it'll save all the other overhead, but it's not the constructor at fault there.
First of all, you're completely right. I do actually mention this fact just before the start of the section "An experiment". Here, the argument is that the bias this odd/even mismatch introduces is orders of magnitudes smaller than what is introduced by the rounding errors; that is, a perfect theoretical RNG drawing from that range would not produce nearly as biased a result (and conversely, if you were to run the snippet in the blog post using `rng.Next(2, int.MaxValue)` or `rng.Next(0, int.MaxValue - 2)`, you wouldn't see the same bias, even though the ranges are still odd/even-biased to about the same extent).
EDIT: I feel like I miss something in the explanation. Can anyone explain how the seed table is actually used?
And thanks for the interesting article btw, just realised you're the author :-)
“Core” and “Framework” have different implementations of the same class? Who names these things?
I've almost started wishing that Microsoft started assigning a reference guid to their products to ease searching about them since many of their products follow the same pattern of having similar names that are only differentiated by a common English word