Generating random numbers (int32) [Answered]
As you may know
Guid.NewGuid()
returns a 128bit random "number" which is very unlikely to get duplicates of.
I need the same behaviour for 32 bit numbers, what's the best way to achieve that? To just use plain old Random#Next()
? Or use Guid#GetHashCode()
?22 Replies
Unknown User•3y ago
Message Not Public
Sign In & Join Server To View
well the more random the better
so that method would provide the most random numbers in that bit range?
Random#Next works just fine
For .NET (Core) at least
so what would produce "more random" numbers? RandomNumberGenerator or the normal Random class approach?
Think about that question for a second
If something is random, it is random
If it isn't random, it isn't random
And both are random
there are predictable randomnesses
That's not exactly true. The random class produces random numbers according to the seed you give it. If you omit the seed it uses the current time (and maybe some other factors to generate that seed). So the randomness there is predictable.
And you shouldn't use it for cryptographic usecases, for that RandomNumberGenerator is better suited
So my guess is RandomNumberGenerator produces less predictable random numbers but that's why I'm asking here as I'm not an expert
randomness in cryptography is more about not being able to determine state from the output rather than being more random, although in some sense that is the result of being more cryptographically secure
point is for many usecases Random will be just fine, though it's not thread safe if that's a concern for you
Thanks for the explanation
RandomNumberGenerator generates a cryptographically secure random number. Random doesn't, and just meets a certain statistical randomness requirement using some subtractive method
So to sum it up
right?
It depends, are you wanting more predictable or uniform randomness
Well I'm not exactly sure.
Let's say I have both running for x amount of time. Which is more likely to hit the same number in the timespan? If you can say that in general
I'd recommend giving this a read, and picking which fits your purpose best: https://khalidabuhakmeh.com/creating-random-numbers-with-dotnet-core
Sounds like you don't need something focused on security, and instead need more uniformity.
I'm a bit confused now.
What I understand is that the RNG class generates random numbers which aren't predictable (=don't rely on system clock etc) which makes sense. But has a less uniform distribution than the Random class
That's right
It's also faster
So if I understand that correctly, RNG is more likely to produce the same number twitce but you cannot predict it.
The random class is less likely but is predictable hence the seed
is that correct?
That's correct from my understanding
another question then, is the seed from the Random.Shared instance random enough or should I rather use my own random instance and generate a seed each time?
It's good enough
I typically always use Random.Shared, and specify my own seed if I need to know what it will create for some reason, like a test
Got it
thanks for the detailed explanation really appreciate it
✅ This post has been marked as answered!