www.delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1994/09/23/21:36:00

Date: Fri, 23 Sep 1994 17:21:35 -0400 (EDT)
From: Edwin Phillips <flaregun AT strauss DOT udel DOT edu>
To: ZRS <roalz AT varano DOT ing DOT unico DOT it>
Cc: djgpp <djgpp AT sun DOT soe DOT clarkson DOT edu>
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<ndice; 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 */
/****************************************************************************/


- Raw text -


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