c# math random number code example

Example 1: c# random number

int random_number = new Random().Next(1, 10) // Generates a number between 1 to 10

Example 2: c# random

Random rnd = new Random();
for (int ctr = 1; ctr <= 10; ctr++)
   Console.WriteLine(rnd.NextDouble() - 1);

// The example displays output like the following:
//       -0.930412760437658
//       -0.164699016215605
//       -0.9851692803135
//       -0.43468508843085
//       -0.177202483255976
//       -0.776813320245972
//       -0.0713201854710096
//       -0.0912875561468711
//       -0.540621722368813
//       -0.232211863730201

Example 3: c# math random

//Function to get random number
private static readonly Random getrandom = new Random();

public static int GetRandomNumber(int min, int max)
{
    lock(getrandom) // synchronize
    {
        return getrandom.Next(min, max);
    }
}