SLR1 Random Number generation Flashcards
How does C sharp generate random numbers?
C# provides the Random class to generate random numbers based on the seed value.
What would be some code to generate a random number?
Random rnd = new Random();
int num = rnd.Next();
What code would generate multiple random numbers?
Random rnd = new Random();
for (int j = 0; j < 4; j++)
{
Console.WriteLine(rnd.Next());
}
What code would allow me t generate random numbers within a range. E.g., less than 10
Next(int)
e.g., Next(10)
What code would allow me t generate random numbers within a range. (maximum and minimum value)
Next(int min, int max)
How to generate a random decimal number
NextDouble()
How is a seed value generated?
The Random class uses the seed value as a starting value for the pseudo-random number generation algorithm
How des the random class work for seed values?
- Random class uses the system clock to generate its seed value
- This means that each instance of the Random class can generate different random numbers.