How can I define what should be the minimum value in a rand() and the maximum?
(e.g. I want to make a rand() from 85(including it) to 100(including it))
2 replies to this topic
#1
Posted 12 March 2011 - 01:29 AM
|
|
|
#2
Posted 12 March 2011 - 02:41 AM
rand() will return an integer from 0 to 32767 depending on implementation, so you must work with that.
This will essentially return a random number between 1 and 15, then add low again to make it between 85 and 100.
rand () % (100 - 85 + 1) + 85Placing this in to a function is appropriate,
int myRand(int low, int high) {
srand(time(NULL));
return rand() % (high - low + 1) + low;
}The % is the modulus operator to return a remainder, useful for limiting larger numbers.This will essentially return a random number between 1 and 15, then add low again to make it between 85 and 100.
Be sure to read the updated FAQ! || Health is achieved through the same 10,000 steps.
If a suggested code/method fails, informing us is less important than telling us why or what errors occurred.
If a suggested code/method fails, informing us is less important than telling us why or what errors occurred.
#3
Posted 12 March 2011 - 11:58 PM
Thnx a lot.
1 user(s) are reading this topic
0 members, 1 guests, 0 anonymous users


Sign In
Create Account


Back to top









