Random Class in C#
In this post, you will learn how to work with the Random class so that you can use that same knowledge and thus evolve.
- this class represents a pseudorandom number generator and it produces a sequence of numbers that meet certain statistical requirements of randomness.
Random (object).Next() |
Returns a random number and, depending on your signature, is also able to return a random number within a specified range |
using System;
namespace Base {
class RandomClass {
private Random random = new Random();
public void Run() {
Console.WriteLine($"Random number up to 20: {random.Next(21)}");
Console.WriteLine($"Random number between 20 and 40: {random.Next(20, 41)}");
for (int index = 1; index <= 10; index++)
Console.WriteLine($"Random number #{index}: {random.Next()}");
}
}
}
/*
Random number up to 20: 8
Random number between 20 and 40: 39
Random number #1: 1992885730
Random number #2: 1283322657
Random number #3: 226863787
Random number #4: 710252620
Random number #5: 1546596848
Random number #6: 1967725250
Random number #7: 1848910215
Random number #8: 1504322913
Random number #9: 1207920689
Random number #10: 571352235
*/
Don’t forget to watch the video and you can always read this post in Portuguese.