HomeCSharpC# Program to Generate Random Numbers

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

You May Also Like

This C# program calculates and displays an upper triangular matrix based on user input. Problem Statement: The program takes the...
This C# program serves as a demonstration of bitwise operators, which are fundamental operators used for manipulating individual bits in...
This C# program is designed to interchange or swap the columns of a matrix. A matrix is a two-dimensional array...