C# Program to Generate Random Numbers

This is a C# Program to generate random numbers.

Problem Description

This C# Program Generates Random Numbers.

Problem Solution

Here random numbers are generated using the Random class and the next() in it.

Program/Source Code

Here is source code of the C# Program to Generate Random Numbers. The C# program is successfully compiled and executed with Microsoft Visual Studio. The program output is also shown below.

/*
 * C# Program to Generate Random Numbers
 */
using System;
class Program
{
    static void Main()
    {
        Console.WriteLine("Some Random Numbers that are generated are : ");
        for (int i = 1; i < 10; i++)
        {
            Randfunc();
        }
    }
    static Random r = new Random();
    static void Randfunc()
    {
        int n = r.Next();
        Console.WriteLine(n);
        Console.ReadLine();
    }
}
Program Explanation

In this C# program using for loop we are calling the Randfun() function. Create the random variable object ‘r’, then using ‘n’ variable we are calling the next() function. Print the random numbers.

advertisement
Runtime Test Cases
 
Some Random Numbers that are generated are : 
1234567
8754352
9864930
8352048
1920472
2846104
7649207
4928756
9261746

Sanfoundry Global Education & Learning Series – 1000 C# Programs.

If you wish to look at all C# Programming examples, go to 1000 C# Programs.

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
I’m Manish - Founder and CTO at Sanfoundry. I’ve been working in tech for over 25 years, with deep focus on Linux kernel, SAN technologies, Advanced C, Full Stack and Scalable website designs.

You can connect with me on LinkedIn, watch my Youtube Masterclasses, or join my Telegram tech discussions.

If you’re in your 40s–60s and exploring new directions in your career, I also offer mentoring. Learn more here.