✅ 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)
xkcd: Random Number
RFC 1149.5 specifies 4 as the standard IEEE-vetted random number.
I remember seeing something about using xor-shift to get random numbers, but couldn't find a lot on it. 🤔
https://en.wikipedia.org/wiki/Xorshift
Seems simple to implement
Unknown User•4mo ago
Message Not Public
Sign In & Join Server To View
Ah
Unknown User•4mo ago
Message Not Public
Sign In & Join Server To View
Ooooh, I was wondering what's that long number lol
Unknown User•4mo ago
Message Not Public
Sign In & Join Server To View
lol
TeBeCo
REPL Result: Success
Console Output
More...
Compile: 380.625ms | Execution: 95.150ms | React with ❌ to remove this embed.
Unknown User•4mo ago
Message Not Public
Sign In & Join Server To View
Oh thanks for the link. I didn't realize that is literally what it is called. Or that it seems pretty easy to implement. I might give that a go and see how it performs.
Still a bit surprised there isn't more libraries for C# that deal with random numbers and randomness (just in general)
Angius
REPL Result: Success
Console Output
Compile: 469.303ms | Execution: 109.025ms | React with ❌ to remove this embed.
Unknown User•4mo ago
Message Not Public
Sign In & Join Server To View
This should be thread-safe
99% of the time it's just not needed
Unknown User•4mo ago
Message Not Public
Sign In & Join Server To View
Yeah, guess that is true. So... not that surprising haha
Huh interesting, thanks for the link
Btw, just so we know what we're working with, what's the .NET version you're using?
I'm using .NET 8.0
Good, good
xorshift is an option if you want to sacrifice even randomness for performance
Unknown User•4mo ago
Message Not Public
Sign In & Join Server To View
Stop asking about XorShift, just use
System.Random
and probably also Random.Shared.Next....
quote from the article bellow:
Despite the similar-sounding name, the “Xoroshiro/Xoshiro” algorithms approaches the problem differently from the preceding “Xorshift” family.
Note: Versions of the Xoshiro number generator are used in the System.Random class in the forthcoming .NET 6.x onwardshttps://blogs.siliconorchid.com/post/coding-inspiration/randomness-in-dotnet
Unknown User•4mo ago
Message Not Public
Sign In & Join Server To View