delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1998/02/06/02:02:31

Date: Thu, 5 Feb 1998 20:54:59 -0800 (PST)
Message-Id: <199802060454.UAA04106@adit.ap.net>
Mime-Version: 1.0
To: "JCx" <jicehix AT worldnet DOT fr>, djgpp AT delorie DOT com
From: Nate Eldredge <eldredge AT ap DOT net>
Subject: Re: How to limit a random() with DJGPP ?

At 11:34  2/5/1998 +0100, JCx wrote:
>Hi ! I started coding some kind of game a few days ago, and was surprised by
>how easy it was to move from Turbo Pascal to C. Allegro is such a wonderful
>piece of code that everything is made easy, even for newcomers. In fact, all
>my problems come from DJGPP itself. And the most important one is that I
>can't find a way to call a limited random(). Was random(10) or anything else
>in Turbo Pascal... what about the same thing with DJGPP ? Thanks to anyone
>who could help me !
Egad, this question is coming up a lot.

`random' returns a value between 0 and the #defined constant `MAXINT'. The
simple way to get a random value between 0 and X is:

random() % X

Another way, which may or may not give more randomness:

(int)(((double)random()) / ((double)MAXINT) * ((double)X))

Note that this is parenthesized and casted VERY defensively (the precedence
and promotion rules escape me at the moment).

Don't forget to seed the RNG (`srandom')!

Nate Eldredge
eldredge AT ap DOT net



- Raw text -


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