✅ Faster & thread safe random number generator?
Is there a good library for getting pseudo random numbers, faster than System.Random? I've done some looking but had a hard time coming up with much.
37 Replies
Do you find the speed of
System.Random
a bottleneck?Yeah, not a massive one, but definitely is one.
Are you using multiple instances of
Random
, or Random.Shared
?
Or some singleton you yourself madeI have a large process which I create a new instance of
Random
for each time, since the process runs in a Task in a separate thread.Random.Shared
is thread-safe, IIRCRandom.Shared Property (System)
Provides a thread-safe Random instance that may be used concurrently from any thread.
Angius
REPL Result: Success
Console Output
Compile: 356.154ms | Execution: 96.933ms | React with ❌ to remove this embed.
Well this got one empty line lmao
Could just be Modix though
Ah good to know, but I actually reset the seed each time, which is why I create a new instance each time.
You need deterministic random?
Yeah
Then I don't think you'll get much faster than
System.Random
Though you can always look for some nugers
Maybe there's something that provides slightly worse randomness but is more performant
I only know a less-performant way, RandomNumberGenerator
, but this one's cryptographically secureYeah I don't really mind if it is a bit worse randomness (I think at least haha)
I remember seeing something about using xor-shift to get random numbers, but couldn't find a lot on it. 🤔