In this article, we will learn how to generate Random Numbers using Random Class in C#. The Random class is used to create random numbers (Pseudo-random that is of course.), that reside in the "System" Namespace. It is easy to use.
Here are the various examples for generating a random number in c#.
using System;
namespace Tutorialsrack
{
class Program
{
/* C# Program To Print Random Number */
static void Main(string[] args)
{
Random rand = new Random();
// creates a number between 1 and 12
Console.WriteLine("Random Month Number is {0}", rand.Next(1, 13));
// creates a number between 1 and 6
Console.WriteLine("Random Dice Number is {0}", rand.Next(1, 7));
// creates a number between 0 and 51
Console.WriteLine("Random Card from Deck is {0}", rand.Next(1,52));
Console.ReadKey();
}
}
}
Random Month Number is 5
Random Dice Number is 6
Random Card from Deck is 30
In the above example 1, we use the Random and Next() method with 2 arguments.
Argument 1: The 1st argument to the Next() method is the inclusive minimum number allowed.
Argument 2: And this argument is an exclusive maximum number. So it never occurs in the output—all numbers must be lower than this argument.
Unhandled Exception: System.ArgumentOutOfRangeException:
'minValue' cannot be greater than maxValue.
Parameter name: minValue
at System.Random.Next(Int32 minValue, Int32 maxValue)
at Program.Main()...
using System;
namespace Tutorialsrack
{
class Program
{
/* C# Program To Print Random Number using Static Random */
static Random _random = new Random();
static void Main(string[] args)
{
// Call a method that uses class-level Random.
RandomNumberUsingStatic();
// Calling a same method still generating the random sequence.
RandomNumberUsingStatic();
// Calling a same method still generating the random sequence.
RandomNumberUsingStatic();
// Calling a same method still generating the random sequence.
RandomNumberUsingStatic();
Console.ReadKey();
}
static void RandomNumberUsingStatic()
{
// Use class-level Random.
// When this method is called many times, it still has good Randoms.
int RandomNumber = _random.Next();
// If this method declared a local Random, it would repeat itself.
Console.WriteLine("STATIC RANDOM: " + RandomNumber);
}
}
}
USING STATIC RANDOM: 2085493745
USING STATIC RANDOM: 910018266
USING STATIC RANDOM: 1056191625
USING STATIC RANDOM: 742393096
In the above example 2, we use static random class instances that improve programs. A method that uses this will still get good random numbers.
using System;
namespace Tutorialsrack
{
class Program
{
/* C# program to print Random Number with parameter */
static void Main(string[] args)
{
RandomNumberWith1Argument();
RandomNumberWith1Argument();
RandomNumberWith1Argument();
RandomNumberWith1Argument();
Console.ReadKey();
}
static Random rand = new Random();
static void RandomNumberWith1Argument()
{
int number = rand.Next(50);
// Can return 0 to 49.
Console.WriteLine("Random Number using 1 argument: {0} ",number);
}
}
}
Random Number using 1 argument: 26
Random Number using 1 argument: 34
Random Number using 1 argument: 17
Random Number using 1 argument: 20
In the above example 3, it uses the Random and Next() method with single arguments.
And this single argument uses the Next() method is the MaxValue which is an exclusive maximum number. So it never occurs in the output—all numbers must be lower than this argument.
using System;
namespace Tutorialsrack
{
class Program
{
/* C# program that causes repeated random numbers
Bad Example for Generating Random Number that causes
Repeated Number.
*/
static void Main(string[] args)
{
RepeatedRandomNumber();
RepeatedRandomNumber();
RepeatedRandomNumber();
RepeatedRandomNumber();
RepeatedRandomNumber();
RepeatedRandomNumber();
Console.ReadKey();
}
static void RepeatedRandomNumber()
{
// Create random and use it.
// This is time-dependent, so can repeat if called many times.
Random rand = new System.Random();
Console.WriteLine("Repeated Number: " + rand.Next());
}
}
}
Repeated Number: 1251196444
Repeated Number: 1251196444
Repeated Number: 1251196444
Repeated Number: 1251196444
Repeated Number: 1251196444
Repeated Number: 1251196444
In this example 4, this program generates a repeated number if called many times, so this is a bad example to use the random class to generate random numbers. So do not use this code in a real program.
I hope this article will help you to understand how to generate Random Numbers using Random Class in C#.
Share your valuable feedback, please post your comment at the bottom of this article. Thank you!
Comments