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

Date: Fri, 23 Sep 1994 14:19:31 +0200 (MET DST)
From: ZRS <roalz AT varano DOT ing DOT unico DOT it>
Subject: Problem with rand() (fwd)
To: djgpp <djgpp AT sun DOT soe DOT clarkson DOT edu>

Never touched the list??? Let's forward!

ZRS  (roalz AT varano DOT ing DOT unico DOT it)


---------- Forwarded message ----------
Date: Mon, 19 Sep 1994 11:05:29 +0200 (MET DST)
From: ZRS <roalz AT varano DOT ing DOT unico DOT it>
To: djgpp <djgpp AT sun DOT soe DOT clarkson DOT edu>
Subject: Problem with rand()

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)




- Raw text -


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