Breaking News
Home / .NET / CSharp / CSharp Snippets / How to generate a random number in CSharp?

How to generate a random number in CSharp?

In order to generate a random number between two numbers, use the Next Function of the Random class as shown below:

Example :

To simulate a roll of a dice, we would need to generate a random integer between 1 and 6 (6 not included).

Random random = new Random();
int randomNumber = random.Next(1, 6);

[pb_builder]

Leave a Reply