-
Random Number Generation
- Produce a number within some bounds following some statistical rules.
- A true random number is a number that is nondeterministically selected from a set of numbers wherein each possible selection has an equal probability of occurrence.
- Usually in computer science we contend with pseudo-random numbers. These are not truly nondeterministic, but an approximation of random selection based on some algorithm.
- Since pseudo-random selections are “determined” by an algorithm, or set of rules, they are technically deterministic.
-
Random Class in C#
-
Instantiate a random number generator and use to select numbers:
-
Notice that we can create a generator with or without an argument. The argument is called a seed for the generator.
-
A seed tells the generator where to start its sequence. Using the same seed will always reproduce the same sequence of numbers.
-
The default constructor still has a seed value, but it is a hidden value pulled from the clock time during instantiation.
-
Time-based seeds only reset approximately every 15 milliseconds.
-
The random class is not “random enough” for cryptography.
-
For cryptographic randomness, use the RNGCryptoServiceProvider class or System.Security.Cryptography.RandomNumberGenerator.
-
-
Using Random
-
Next() method returns a pseudo-random number between 0 and 2,147,483,647 (max signed
int
), inclusive. -
By default, the number is always non-negative and within that range.
-
What if we wanted to create a random number between 0 and 100?
-
We could use rand.Next() and then use modulo to cut down the answer range!
-
Alternatively, we could give the Next() method an
int
argument to set a ceiling. -
The ceiling value is exclusive, so remember to use one number higher than what you want to be your max number.
-
We can also pass two arguments in order to set a range for the values.
-
The ceiling value is still exclusive, but the floor is inclusive.
-
NextDouble() returns a normalized value (value between 0.0 and 1.0 inclusive).
-
What if we want a different range? Adjust with math!
-
NextBytes() method takes a
byte
array as an argument and generates a randombyte
value for each index. -
Remember, a
byte
has an unsigned value between 0 and 255 inclusive.
-
-
Creating Random Strings
-
What if we want to construct random strings made of a, b, c, and d?
-
Other techniques are available, but we can use a loop and switch!
-