Dofactory.com
Dofactory.com

How to generate a random int number in C#?

To create a random number in C# use the Random class.

If you need more than one random number keep the Random instance and reuse it. Creating new instances too close to each other will produce the same series of random numbers as the random generator is seeded from the system clock.

Here is an example where the same instance is reused:


var random = new Random();

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


Jack Poorte
Jack Poorte
Last updated on Sep 30, 2023



Stay Inspired!
Join other developers and designers who have already signed up for our mailing list.
Terms     Privacy     Cookies       Do Not Sell       Licensing      
Made with    in Austin, Texas.  - vsn 44.0.0
© Data & Object Factory, LLC.