Simple rand() question

Status
Not open for further replies.

petrov

Ars Tribunus Militum
2,714
Originally posted by LimpBagel:
Whooosh. Right over my head
:confused:

he said this:
code:<HR>
Code:
float num = ((float) rand()) / MAX_INT;

// but probably meant this:
float num = ((float) rand()) / RAND_MAX;

you probably want to use doubles rather than floats as memory is cheap. If you want a random number in a range other than 0->1, then multiply num by the size of the range, and add whatever offset you want.

--sam
 

bash666

Ars Legatus Legionis
24,417
you probably want to use doubles rather than floats as memory is cheap. If you want a random number in a range other than 0->1, then multiply num by the size of the range, and add whatever offset you want.

Yes, I did mean the second. But I assumed that rand returned int and spanned then entire range of INT, which woudl make MAX_RAND=MAX_UINT

Cheers,
Kevin
 
Status
Not open for further replies.