www.delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1994/09/26/18:25:09

Date: Mon, 26 Sep 1994 14:25:42 -0400
From: Cuthalion / Sliced Bread <enrico AT max DOT tiac DOT net>
To: richard DOT young AT clark DOT dgim DOT doc DOT ca, roalz AT varano DOT ing DOT unico DOT it
Subject: Re: Correction to (Re: Problem with rand() (fwd))
Cc: djgpp AT sun DOT soe DOT clarkson DOT edu

..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 -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019