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

Popular posts from this blog

Django REST Framework perform_create: You cannot call `.save()` after accessing `serializer.data` -

Why does Go error when trying to marshal this JSON? -