mardi 30 août 2016

Make sure that random() does not return 0?

I'm looking at the documentation for random():

http://ift.tt/2bCxtLj

It returns successive pseudo-random numbers in the range from 0 to (2**31)-1.

I don't want it to return 0 ever.

I'm thinking about writing:

long rand = random() + 1;

But if I'm not mistaken, long can be 32-bits on a 32-bit processor. I guess I would risk stack overflow then.

What is the best approach to getting a random number between 1 and (2**31)-1?




Aucun commentaire:

Enregistrer un commentaire