Mail Archives: djgpp/1997/02/25/01:06:59
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 -