Mail Archives: djgpp/1998/02/06/02:02:31
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 -