Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Random number that divides by 5

Random random = new Random();
int randomx = random.Next(0, 240);

This is the way I get my random number, from 0 to 240, how can get only integrals that divide with 5? (0 included)

0, 5, 10, 15, 20, 25 .. 240

like image 948
Badr Hari Avatar asked Nov 27 '22 06:11

Badr Hari


2 Answers

How about this:

Random random = new Random();
return random.Next(0, 48) * 5;

Or, if you need 240 included, as your list indicates:

Random random = new Random();
return random.Next(0, 49) * 5;
like image 116
marcind Avatar answered Dec 14 '22 22:12

marcind


Here's one (very bad, hence the community wiki) way to do it:

Random random = new Random();
int randomx = 1;
while ((randomx % 5) > 0)
    randomx = random.Next (0,240);

:-)

Feel free to downvote this answer into oblivion. It's really just to prevent others from posting it.

like image 42
2 revs Avatar answered Dec 14 '22 23:12

2 revs