Mail Archives: djgpp/1999/09/01/15:30:36
Nate Eldredge <neldredge AT hmc DOT edu> wrote in message
news:37CC9150 DOT 2BFD0245 AT hmc DOT edu...
> Tom Morton wrote:
> > Hello fellow human beings,
> > I'm writing a game with allegro and djgpp and it requires a random
> > number generator that can have it's seed saved and changed. Until
> > now I have been using the rand() and srand() functions but I need
> > to be able to store the state of the random number generator.
Easy. Use this pseudorandom number function (adapted
from the ANSI standard for C rand() ) and two accessors:
/**** cut ****/
static int curSeed = 1;
* Returns a random number from 0 to 32,767.
curSeed = (curSeed * 1103515245) + 12345;
return (curSeed & 0x7fff0000) >> 16;
* Replaces srand(). Sets the seed for MyRandom().
void SetSeed(int seed)
curSeed = seed;
* Returns the current seed for MyRandom() so it can be saved.
/**** and paste ****/
If you are using Allegro's install_timer(), an initial seed can be obtained
Assuming you have a struct or class with a member
randSeed, and you have an instance thereof called myState:
To save the seed,
myState.randSeed = GetSeed();
To restore the seed,
> Nate Eldredge
> neldredge AT hmc DOT edu
- Raw text -