C# Program to Generate Random Numbers

C# provides a convenient way to generate random numbers for various purposes, such as simulations, games, and testing. In this example, we’ll create a C# program to generate random numbers, showcasing the language’s built-in random number generation capabilities.

Program Statement

Write a C# program to generate and display random numbers. The program should generate a specified number of random integers within a given range and display them to the user.

C# Program to Generate Random Numbers

using System;

class Program
{
    static void Main()
    {
        Console.Write("Enter the number of random numbers to generate: ");
        int count = Convert.ToInt32(Console.ReadLine());

        Console.Write("Enter the minimum value: ");
        int min = Convert.ToInt32(Console.ReadLine());

        Console.Write("Enter the maximum value: ");
        int max = Convert.ToInt32(Console.ReadLine());

        Console.WriteLine("Random numbers generated:");
        Random random = new Random();

        for (int i = 0; i < count; i++)
        {
            int randomNumber = random.Next(min, max + 1);
            Console.WriteLine(randomNumber);
        }
    }
}

Input / Output

C# Program to Generate Random Numbers

Share:

Leave A Reply

Your email address will not be published. Required fields are marked *

You May Also Like

C# Compiler Error CS0442 – ‘Property’: abstract properties cannot have private accessors Reason for the Error You’ll get this error...
This is a really simple one . Below is a simple example of an enum called “Designation” defined with the...
This blog post explain the usage of the Checked Block in .NET and how you can use them in Visual...