c# random num code example

Example 1: c# random int

Random rnd = new Random();
int number  = rnd.Next(1, 10);

Example 2: get random number c#

Random rnd = new Random();
int month  = rnd.Next(1, 13);  // creates a number between 1 and 12
int dice   = rnd.Next(1, 7);   // creates a number between 1 and 6
int card   = rnd.Next(52);     // creates a number between 0 and 51

Example 3: generate a random number in c#

Random rnd = new Random();
int value = rnd.Next(min,max);
Console.Write(value);

Example 4: c# random

Random rnd = new Random();
for (int ctr = 0; ctr < 10; ctr++) {
   Console.Write("{0,3}   ", rnd.Next(-10, 11));
}

// The example displays output like the following:
//    2     9    -3     2     4    -7    -3    -8    -8     5