generate a random number in csharp

To generate a random number in C#, you can use the Random class in the System namespace. Here's an example:

main.cs
Random rnd = new Random();
int randomNumber = rnd.Next(1, 101); // generate a random number between 1 and 100
110 chars
3 lines

In this example, a new instance of Random is created and assigned to the rnd variable. The Next method is then called on rnd, which generates a random integer between the first and second arguments passed to the method. In this case, the random number will be between 1 and 100 (inclusive).

You can adjust the arguments passed to Next to generate a random number within a different range.

gistlibby LogSnag