Date: Fri, 23 Sep 1994 17:21:35 -0400 (EDT) From: Edwin Phillips To: ZRS Cc: djgpp Subject: Re: Problem with rand() (fwd) On Fri, 23 Sep 1994, ZRS wrote: > HI! > Precondition: i have djgpp 1.12 (no maint, yet). > > It seems that rand() returns alternately an even and an odd number > (I.E. 0 141 2900 12717 ...) > > 1.) Is this the recommended (ANSI) behaviour ?? > > 2.) This causes me problems in a "Die" C++ class I wrote yesterday, where > I have a method: > inline unsigned int roll(const unsigned int ndice) > { > unsigned int result = 0; > for(int i=0; i { > result += ((rand() % _sides) + 1); > } > return result; > } > > (Yes, I DO HAVE srand(time(0)) in the constructor!) > > This is the simplest and more portable way I can think of to be just like > a 'real' dice roll-over !! > > BUT... I can get only EVEN numbers for 'result'. > I do know I could use RNGs from libg++, but then I must have libg++ > anywhere the Die is ported! > > Help, please... > > P.S. The same Die class works greatly under Linux op.sys. when compiled > with gcc version 2.5.8 !! > > ZRS (roalz AT varano DOT ing DOT unico DOT it) > > Have you tried the ?rand48() routines in libc? This is a better pseudo-random generator than rand() is based on. Hope this helps, Ed /****************************************************************************/ /* Ed Phillips flaregun AT strauss DOT udel DOT edu University of Delaware */ /* Jr Systems Programmer (302) 831-6082 IT/Network and Systems Services */ /****************************************************************************/