C# program to generate random numbers

Generating random numbers in C#: Here, we are going to learn how to generate a random number using Next() method in C#?
Submitted by Nidhi, on August 17, 2020

Here we used Random class to generate random numbers using the Next() method of the random class.

Program:

The source code to generate random numbers in C# is given below. The given program is compiled and executed successfully on Microsoft Visual Studio.

//Program to Generate Random Numbers in C#.

using System;

class RandomEx
{
    static void Main()
    {
        int num = 0;
        Random R = new Random();

        Console.WriteLine("Random Numbers: ");
        for (int i = 1; i < 10; i++)
        {
            num = R.Next();
            Console.WriteLine(num);    
        }
    }
}

Output:

Random Numbers:
1396315331
747908140
1390110439
2025904104
318255387
1630415135
455253731
1489207714
137356946
Press any key to continue . . .

Explanation:

In the above program, we created the RandomEx class that contains Main() method, In the Main() method we created the object R of Random class.

Console.WriteLine("Random Numbers: ");
for (int i = 1; i < 10; i++)
{
    num = R.Next();
    Console.WriteLine(num);    
}

Here we used the Next() method to generate random numbers and then print them on the console screen.






Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.





Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates


© https://www.includehelp.com some rights reserved.