Hi It is possible to generate random number within a range (-x,x) using rand()?? If not, how can I generate random number withing that range ?
See Question&Answers more detail:osHi It is possible to generate random number within a range (-x,x) using rand()?? If not, how can I generate random number withing that range ?
See Question&Answers more detail:os// return a random number between 0 and limit inclusive.
int rand_lim(int limit) {
int divisor = RAND_MAX/(limit+1);
int retval;
do {
retval = rand() / divisor;
} while (retval > limit);
return retval;
}
// Return a random number between lower and upper inclusive.
int rand_lim(int lower, int upper) {
int range = abs(upper-lower);
return rand_lim(range) + lower;
}
As usual, all the others I've seen in this thread can/will produce at least slightly skewed results.