find and solve || findandsolve.com
Please wait.....

C# Random Example

In the c# random class takes seed values from your CPU clock which is very much predictable. So in other words RANDOM class of C# generates pseudo random numbers or string.C# provides the Random class to generate random numbers or string based on the seed value.


MethodDescription
Next() Returns a positive random integer within the default range -2,147,483,648 to 2,147,483, 647.
Next(int) Returns a positive random integer that is less than the specified maximum value
Next(int, int) Returns a positive random integer within the specified minimum and maximum range (includes min and excludes max).
NextDouble() Generates random floating-point number that is greater than or equal to 0.0 and less than 1.0.
NextByte()Fills the specified array with the random bytes.
using System;
public class Program
{     // Main Method
    public static void Main(string[] args)
    {
       Random rnd = new Random();
       int a = rnd.Next(1, 18);
       int b = rnd.Next(1, 8);
       int c = rnd.Next(63);
    Console.WriteLine("creates a number between 1 and 19 {0} : ",a);
    Console.WriteLine("creates a number between 1 and 9 {0} : ",b);
    Console.WriteLine("creates a number between 0 and 63 : {0}",c);   
    }
}

Generate Random Numbers in C#

using System;
public class Program
{
    // Main Method
    public static void Main(string[] args)
    {
       Random rnd = new Random();
        int num = rnd.Next();
  Console.WriteLine("The Random Numbre is {0}",num);
    }
}

Output

The Random Numbre is 862939083

Generate Multiple Random Number in C#

using System;
public class Program
{
 public static void Main()
 {
  Random rnd = new Random();
  for (int i = 0; i < 7; i++)
  {
   Console.WriteLine("Loop Random Number is {0}",rnd.Next());
  }
 }
}

Output

Loop Random Number is 2075106199

Loop Random Number is 2135806284

Loop Random Number is 2097887620

Loop Random Number is 1964060340

Loop Random Number is 222292997

Loop Random Number is 238491327

Loop Random Number is 2103145409

Generate Random Numbers in Range in C#

Use the Next(int) method overload to generate a random integer that is less than the specified maximum value.The following example generates the positive random numbers that are less than 8.

using System;
public class Program
{
 public static void Main()
 {
  Random rnd = new Random();
  for (int i = 0; i < 4; i++)
  {
   Console.WriteLine(rnd.Next(10)); //returns random Number < 8
  }  }
}

Output

6

4

7

9

Generate Random Number in Min to Max Range in C#

Use the Next(int min, int max) overload method to get a random integer that is within a specified range.

using System;
public class Program
{
 public static void Main()
 {
  Random rnd = new Random();
  for (int i = 0; i < 5; i++)
  {
   Console.WriteLine("Random integers >= 15 and < 35 : {0} ",rnd.Next(15, 35));
  }
 }

Output

Random integers >= 15 and < 35 : 21

Random integers >= 15 and < 35 : 15

Random integers >= 15 and < 35 : 17

Random integers >= 15 and < 35 : 21

Random integers >= 15 and < 35 : 15 

Generate Random Floating Point Number in C#

using System;
public class Program {
 public static void Main()
 {
  Random rnd = new Random();
  for (int i = 0; i < 5; i++)
  {
   Console.WriteLine("Generate Random Floats : {0}", rnd.NextDouble());
  }
 }
}

Output

Generate Random Floats : 0.9789970657204249

Generate Random Floats : 0.2154347323548933

Generate Random Floats : 0.7391019587184966

Generate Random Floats : 0.007164717818438637

Generate Random Floats : 0.1916866930105876

Generate Random Bytes in C#

In the c# use NextBytes() method to generate a series of byte values. Pass an array to fill the number of byte values.In the given below  example shows how to generate a random number using NextBytes() method in c# programming language.

using System;
public class Program
{
 public static void Main()
 {
  Random rnd = new Random();
  byte[] randomBytes = new byte[5];
  rnd.NextBytes(randomBytes);
  foreach (byte item in randomBytes)   {
   Console.WriteLine("Generate Random Bytes : {0}",item);
  }
 }
}

Output

Generate Random Bytes : 235

Generate Random Bytes : 230

Generate Random Bytes : 56

Generate Random Bytes : 66

Generate Random Bytes : 54

Seed Value in Random Class in c#

using System;
public class Program
{
 public static void Main()
 {
  Random objRandomFirst = new Random(20);
  for (int i = 0; i < 5; i++)
  {
   Console.WriteLine("seed value 20 : {0}",objRandomFirst.Next());
  }
  Console.WriteLine("Another Second Example");
  Random rnd2 = new Random(20);
  for (int j = 0; j < 5; j++)
  {
   Console.WriteLine("Second Example : {0}",rnd2.Next());
  }
 } }

Output

seed value 20 : 375271809

seed value 20 : 1472524622

seed value 20 : 1605850688

seed value 20 : 1776011503

seed value 20 : 506264866

Another Second Example

Second Example : 375271809

Second Example : 1472524622

Second Example : 1605850688

Second Example : 1776011503

Second Example : 506264866