*crying* is it possible to make a max number for the rand comand its going to high!!!!! *crying*

Printable View

- 04-05-2002mill1000Help!!!!!!!!!!!!!!
*crying* is it possible to make a max number for the rand comand its going to high!!!!! *crying*

- 04-05-2002xlnk
yes if you do this:

Code:`rand() % 10;`

if you do this:

Code:`(rand() % 10) + 3;`

- 04-05-2002elad
well....almost:

rand() % 10 will return numbers 0 - 9 inclusive;

and (rand % 8) + 3 will return return numbers 3 - 10 inclusive; - 04-05-2002mill1000
this is kinda stupid but wat does inclusive mean

and it din't work - 04-05-2002asdf
inclusive = numbers from 0 to 9 and including 0 and 9.

Why doesn't it work? Does it compile?

Did you include stdlib? I think the rand function is in ther - 04-05-2002mill1000
it din't minimize the rand cout i wanted it between 140 - 160 but it still went higher no as high but it went to about 700

- 04-05-2002Imperito
Well, lets see the way you have it written.

But lets simplify it first:

Try making a for loop that sends a random number between 140 and 170 to cout, then a '\t'. Make it iterate 50 times, and post the source if it does not do what you want it to. - 04-05-2002asdf
if you want random values in a range try the formula

Code:`a + rand() % (b-a+1)`

b = upper range

In your case a= 140, b = 160 - 04-05-2002ErionD
Just use the random func from the FAQ.

Code:`int random(int low, int high) { // my random function`

srand(time(NULL));

return low + rand()%(high-low+1);

}