Generating random numbers in C#



To generate random numbers, use Random class.

Create an object −

Random r = new Random();

Now, use the Next() method to get random numbers in between a range −

r.Next(10,50);

The following is the complete code −

Example

 Live Demo

using System; public class Program {    public static void Main() {       Random r = new Random();       int genRand= r.Next(10,50);       Console.WriteLine("Random Number = "+genRand);    } }

Output

Random Number = 24
Updated on: 2020-06-22T12:51:46+05:30

3K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements