Random Number Generator in C#
In C#, you can use the Random class to generate random numbers. Here's a simple example that generates a random integer between 1 and 10, inclusive:
using System;
class Program
{
static void Main()
{
Random rnd = new Random(); // Create a new Random object
int randomNumber = rnd.Next(1, 11); // Generate a random number between 1 and 10
Console.WriteLine($"Random Number: {randomNumber}"); // Print the random number
}
}
In this code:
- Random rnd = new Random(); creates a new instance of the Random class.
- rnd.Next(1, 11); generates a random integer that is at least 1 and less than 11, effectively giving you a random number between 1 and 10, inclusive.
- Console.WriteLine() is used to output the random number to the console.