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: 22-Jun-2020

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements