C
C#3y ago
Dusty

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
Unknown User3y ago
Message Not Public
Sign In & Join Server To View
Dusty
DustyOP3y ago
well the more random the better so that method would provide the most random numbers in that bit range?
Monsieur Wholesome
Random#Next works just fine For .NET (Core) at least
Dusty
DustyOP3y ago
so what would produce "more random" numbers? RandomNumberGenerator or the normal Random class approach?
Monsieur Wholesome
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
ero
ero3y ago
there are predictable randomnesses
Dusty
DustyOP3y ago
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
Aeon Vex
Aeon Vex3y ago
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
Dusty
DustyOP3y ago
Thanks for the explanation
Auger
Auger3y ago
RandomNumberGenerator generates a cryptographically secure random number. Random doesn't, and just meets a certain statistical randomness requirement using some subtractive method
Dusty
DustyOP3y ago
So to sum it up
// Overall more "random"
return RandomNumberGenerator.GetInt32(int.MaxValue);
// than this
return Random.Shared.Next(int.MaxValue);
// Overall more "random"
return RandomNumberGenerator.GetInt32(int.MaxValue);
// than this
return Random.Shared.Next(int.MaxValue);
right?
Auger
Auger3y ago
It depends, are you wanting more predictable or uniform randomness
Dusty
DustyOP3y ago
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
Auger
Auger3y ago
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.
Dusty
DustyOP3y ago
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
Auger
Auger3y ago
That's right It's also faster
Dusty
DustyOP3y ago
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?
Auger
Auger3y ago
That's correct from my understanding
Dusty
DustyOP3y ago
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?
Auger
Auger3y ago
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
Dusty
DustyOP3y ago
Got it thanks for the detailed explanation really appreciate it
Accord
Accord3y ago
✅ This post has been marked as answered!
Want results from more Discord servers?
Add your server