\hey everyone.. i'm looking it up, and i can find ways to randomize a number from 0 to whatever, but how do you get a random number from lets say -500 to 500?
any help would be appreciated, thanks
\hey everyone.. i'm looking it up, and i can find ways to randomize a number from 0 to whatever, but how do you get a random number from lets say -500 to 500?
any help would be appreciated, thanks
Try this:
Code:int R(int min, int max) { return (rand() % (max - min)) + min; }
That code you posted gives a rand from min to max-1.
If you input, say, 0 as min, and 10 as max, it would be equal to rand()%10 + 0 -> which gives 0-9, not 1-10.
To get a number between min and max inclusive, use this:
Code:int R(int min, int max){ return (rand() % (max + 1 - min)) + min; }
True. Whether you want inclusive or exclusive on either or both ends depends on the application, I suppose, but probably it's most logical to include that extra +1, as you probably wouldn't want R(0,1) to always return zero.