c - Random number generation in my implementation -
is implementation of random number generation wrong?
int random_number(int l, int u) { int num; num = rand()%u; if (num<l && num>u) { while(num<l && num>u) { num = rand()%u; } } return num; }
this not giving me correct answer. if try random_number(4,8); generates numbers 0,1,2 etc.
assuming u
means upper , l
means lower, wrong. try this:
int random_number(int l, int u) { int num = rand() % (u - l); return num + l; }
Comments
Post a Comment