www.delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1997/02/25/01:06:59

From: "John M. Aldrich" <fighteer AT cs DOT com>
Newsgroups: comp.os.msdos.djgpp
Subject: Re: Error??
Date: Mon, 24 Feb 1997 23:39:28 -0800
Organization: Two pounds of chaos and a pinch of salt
Lines: 44
Message-ID: <33129730.642B@cs.com>
References: <33123B5C DOT 6605 AT epix DOT net>
Reply-To: fighteer AT cs DOT com
NNTP-Posting-Host: ppp225.cs.com
Mime-Version: 1.0
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp

Mr. Cup O. Slaw wrote:
> 
> I have two programs that use the function radomize(); which I use to
> later represent a dice roll of three six sided dice. Example:
> 
> roll= (random(6)+1) + (random(6)+1) + (random(6)+1);
> 
> and a few others I can't quite remember. Any help would be greatly
> appreciated. And please excuse my lack of knowledge if the answer is an
> obvious one. Thanks.

The answer is indeed obvious if you look up random() in the DJGPP
documentation.  The DJGPP implementation of random() is defined as
follows:

long random(void);

It returns a number from 0 to MAXINT.  To clip this to a useable value,
use the mod operator like so:

roll = random() % 6 + 1 + random() % 6 + 1 + random() % 6 + 1;

Of course, it's better from a stylistic point of view to create a
function or macro to handle a single die roll and call that instead. 
That way you can change the randomization method without rewriting half
your program.

BTW, there is no 'randomize()' in DJGPP.  To seed the random number
generator, use the following:

#include <time.h>
srandom((unsigned) time(NULL));

All of this is explained in the documentation.  If you don't know how to
view the docs, try looking at the 'readme.1st' file, or post here for
more information.

-- 
---------------------------------------------------------------------
| John M. Aldrich, aka Fighteer I  |        fighteer AT cs DOT com         |
|    *  Proud user of DJGPP!  *    |   http://www.cs.com/fighteer   |
|    ObJoke:  If Bill Gates were a robber, not only would he        |
|     shoot you, but he'd send you a bill for the bullets.          |
---------------------------------------------------------------------

- Raw text -


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