I've seen several articles talking about not generating too many System.Random
instances too closely together because they'll be seeded with the same value from the system clock and thus return the same set of numbers:
- https://stackoverflow.com/a/2706537/2891835
- https://codeblog.jonskeet.uk/2009/11/04/revisiting-randomness/
- https://learn.microsoft.com/en-us/dotnet/api/system.random?view=netframework-4.8#instantiating-the-random-number-generator
That appears to be true for .net framework 4.8.
Looking at the source code for .net core, however, it looks like instances of System.Random
generated on the same thread are seeded from a [ThreadStatic]
generator.
So it would seem to me that, in .net core, you are now safe to do something like:
Random[] randoms = new Random[1000];
for (int i = 0; i < randoms.Length; i++)
{
randoms[i] = new Random();
}
// use Random instances and they'll all have distinct generated patterns
Is this true? Am I missing something?
Note: Assume we're talking about a single thread here (although I'm not sure it matters because it looks like .net core uses bcrypt or something to seed the global generator per-thread?).
EDIT: This is now tracked in this ticket.