Earn income with your C# skills
Sign up and we'll send you the best freelance opportunities straight to your inbox.
We're building the largest self-service freelancing marketplace for people like you.

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

Stay Inspired!
Join other developers and designers who have already signed up for our mailing list.
Terms     Privacy     Licensing       EULA       Sitemap      
© Data & Object Factory, LLC.
Made with    in Austin, Texas.      Vsn 1.3.0