C
C#2y 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 User2y ago
Message Not Public
Sign In & Join Server To View
Dusty
Dusty2y 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
Dusty2y 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
ero2y ago
there are predictable randomnesses
Dusty
Dusty2y 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 Vex2y 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
Dusty2y ago
Thanks for the explanation
Auger
Auger2y ago
RandomNumberGenerator generates a cryptographically secure random number. Random doesn't, and just meets a certain statistical randomness requirement using some subtractive method
Dusty
Dusty2y 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
Auger2y ago
It depends, are you wanting more predictable or uniform randomness
Dusty
Dusty2y 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
Auger2y 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
Dusty2y 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
Auger2y ago
That's right It's also faster
Dusty
Dusty2y 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
Auger2y ago
That's correct from my understanding
Dusty
Dusty2y 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
Auger2y 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
Dusty2y ago
Got it thanks for the detailed explanation really appreciate it
Accord
Accord2y ago
✅ This post has been marked as answered!
Want results from more Discord servers?
Add your server
More Posts
Visual Studio doesn't work as expected with source generators [Answered]I wanted to give source generators a try, i followed Microsoft tutorial and everything worked as expBlazor WASM Hosted uses wrong basepath on page reloadHello! I have a web app that mostly works as it should, except on a Page Refresh or direct accessingCalling a static abstract method without a generic type [Answered]If I have this code with a static abstract member `Bar` in the interface `IFoo`, is there *any way* WinUI3 app doesn't run outside of Visual StudioI just tried to share a tool I made in WinUI3 with .NET6. That shouldn't be a problem with the publiLearning Agile and Scrum on my own, looking for advice, resources, insightsI haven't had the chance to work both in groups of developers nor in groups of developers working unProper C multicatch syntaxI saw in Java that you can do multicatch blocks (see image) I also looked it up on stackoverflow: Change output directory of nuget packageUsually, nuget package assemblies and dependencies are copied to OutDir during build, e.g. `bin\DebuCan someone help me? I got this errorHi, so i have table `Laporan_Penilaian` form another `database`. When i tried to run my project i goHow to use custom DateTimeConverter for selected endpoints?I have the following converter which is responsible for reading and returning dates in UTC format: `.NET Program that runs in the background [Answered]How would I make an .exe program in c# .NET that runs in the background (It will have a tray icon in