Date: Thu, 5 Feb 1998 20:54:59 -0800 (PST) Message-Id: <199802060454.UAA04106@adit.ap.net> Mime-Version: 1.0 Content-Type: text/plain; charset="us-ascii" To: "JCx" , djgpp AT delorie DOT com From: Nate Eldredge Subject: Re: How to limit a random() with DJGPP ? Precedence: bulk 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