Mail Archives: djgpp/1994/09/26/18:25:09
..roll += int(float(rand())/(RAND-MAX+1) * _sides) + 1;
..
..This still should not involve a divide since the /(RAND_MAX+1) is a
..constant and would be implemented as a multiply by 1/(RAND_MAX+1).
If you're after speed, you'd probably be better off doing a integer divide, rather than a floating point multiply and truncating. And anyways, your code won't do the / in floating point. (Additionally, RAND_MAX has a _, not a -)
If you want the divide to be fp, you need to do
roll += int (rand()/(float)(R
AND_MAX + 1)) + 1;
Sorry about the wierd placement of CR's.
- Raw text -