Tutorial by Examples

This example generates random values between 0 and 2147483647. Random rnd = new Random(); int randomNumber = rnd.Next();
Generate a random number between 0 and 1.0. (not including 1.0) Random rnd = new Random(); var randomDouble = rnd.NextDouble();
Generate a random number between minValue and maxValue - 1. Random rnd = new Random(); var randomBetween10And20 = rnd.Next(10, 20);
When creating Random instances with the same seed, the same numbers will be generated. int seed = 5; for (int i = 0; i < 2; i++) { Console.WriteLine("Random instance " + i); Random rnd = new Random(seed); for (int j = 0; j < 5; j++) { Console.Write(rnd.Next(...
Two Random class created at the same time will have the same seed value. Using System.Guid.NewGuid().GetHashCode() can get a different seed even in the same time. Random rnd1 = new Random(); Random rnd2 = new Random(); Console.WriteLine("First 5 random number in rnd1"); for (int i = 0...
Generate a random letter between a and z by using the Next() overload for a given range of numbers, then converting the resulting int to a char Random rnd = new Random(); char randomChar = (char)rnd.Next('a','z'); //'a' and 'z' are interpreted as ints for parameters for Next()
A common need for random numbers it to generate a number that is X% of some max value. this can be done by treating the result of NextDouble() as a percentage: var rnd = new Random(); var maxValue = 5000; var percentage = rnd.NextDouble(); var result = maxValue * percentage; //suppose NextDoub...

Page 1 of 1