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

Date: Tue, 25 Feb 1997 13:44:07 -0300
Message-Id: <1.5.4.16.19970225095618.296ffd00@dmeasc.rc.ipt.br>
Mime-Version: 1.0
To: fighteer AT cs DOT com, djgpp AT delorie DOT com
From: Cesar Scarpini Rabak <csrabak AT dce03 DOT ipt DOT br>
Subject: Re: Error??

At 23:39 24/02/97 -0800, John M. Aldrich wrote:
>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;
>

BTW^2, it is also a poor practice to use just the modulo operator in the
return values from random, as the lower bits of the pseudo random number
have less "randomness".

In an earlier posting on this matter I've pasted an excerpt from the C
Language FAQ which discuss this in fully. I recommend people interested look
for it in the DJ mail archives.

>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.          |
>---------------------------------------------------------------------
>
>
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Cesar Scarpini Rabak                      E-mail: csrabak AT ipt DOT br
DME/ASC                                   Phone: 55-11-268-3522 Ext.350
IPT - Instituto de Pesquisas Tecnologicas Fax:   55-11-268-5996
Av. Prof. Almeida Prado, 532.  Sao Paulo - SP 05508-901 BRAZIL
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

- Raw text -


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