0

How to generate random number?

How can I generate random number?

12th Sep 2016, 6:51 AM
Ilyosjon Kamoldinov
Ilyosjon Kamoldinov - avatar
2 Answers
+ 1
It depends on what type of number you want to be returned; is it integer or decimal? If you need Integer, use this: Random r = new Random(); int randomInt = r.Next(range); For example, you want to get a Random Integer from 0-9: Random r = new Random(); int randomNumber = r.Next(10); 10 represents the range starting from 0. 0-9 consists of 10 numbers. If you want a random integer from 1-10: Random r = new Random(); int randomNumber = r.Next(10) + 1; It will add 1 from the generated random number so the options of the random number to be returned is from 1-10. Now, if you need a Random Decimal Number, use this: Random r = new Random(); double randomDec = r.NextDouble();
12th Sep 2016, 7:13 AM
Erwin Mesias
Erwin Mesias - avatar
- 1
Computers can't generate real random numbers...
14th Sep 2016, 6:24 AM
Pablo Hernandez