Generating random numbers in C#

CsharpProgrammingServer Side Programming

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
raja
Published on 31-Aug-2018 11:46:54
Advertisements