I was just wondering how the random number generator in C# works. I was also curious how I could make a program that generates random WHOLE INTEGER numbers from 1-100.
相关问题
- Sorting 3 numbers without branching [closed]
- Graphics.DrawImage() - Throws out of memory except
- Why am I getting UnauthorizedAccessException on th
- 求获取指定qq 资料的方法
- How to know full paths to DLL's from .csproj f
I've been searching the internet for RNG for a while now. Everything I saw was either TOO complex or was just not what I was looking for. After reading a few articles I was able to come up with this simple code.
Simple explanation,
This works well.
To obtain a random number less than 100 use
and so on for 3, 4, 5, and 6 ... digit random numbers.
Hope this assists someone positively.
You can use
Random.Next(int maxValue)
:For this case however you could use
Random.Next(int minValue, int maxValue)
, like this:That's implementation-specific, but the wikipedia entry for pseudo-random number generators should give you some ideas.
You can use
Random.Next(int, int)
:Note that the upper bound is exclusive - which is why I've used 101 here.
You should also be aware of some of the "gotchas" associated with
Random
- in particular, you should not create a new instance every time you want to generate a random number, as otherwise if you generate lots of random numbers in a short space of time, you'll see a lot of repeats. See my article on this topic for more details.