jeudi 24 septembre 2015

Random number between INT_MAX & INT_MIN

Well I feel my question is bit more complex compared to other questions. I realized this while trying to solve it.

I tried using

int number = rand() % (INT_MAX - INT_MIN + 1) + INT_MIN;
rand() % (INT_MAX + INT_MIN + 1) + rand() % INT_MIN;

However, I got floating point exception 8 error. No warnings, that's really strange!

Also, from time.h, I used srand((unsigned)time(NULL)) for new random number every time I execute my code.

But whatever I try either I get incorrect results or floating point exception.

I am really curious as to overflow & underflow, how it's happening and can such a random number be actually generated?

I basically want to generate a number in C greater than INT_MIN and lesser than INT_MAX.

I tried lots of logic but I got incorrect result.




Aucun commentaire:

Enregistrer un commentaire