delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1999/09/01/15:30:36

From: "Damian Yerrick" <die DOT spammers AT pineight DOT 8m DOT com>
Newsgroups: comp.os.msdos.djgpp
Subject: Re: Saving random seed, and Allegro
Date: Tue, 31 Aug 1999 23:58:31 -0500
Organization: Rose-Hulman Institute of Technology
Lines: 64
Message-ID: <7qibto$oqr$1@solomon.cs.rose-hulman.edu>
References: <7qhljj$245$3 AT news6 DOT svr DOT pol DOT co DOT uk> <37CC9150 DOT 2BFD0245 AT hmc DOT edu>
NNTP-Posting-Host: 137.112.205.182
X-Trace: solomon.cs.rose-hulman.edu 936162040 25435 137.112.205.182 (1 Sep 1999 05:00:40 GMT)
X-Complaints-To: news AT cs DOT rose-hulman DOT edu
NNTP-Posting-Date: 1 Sep 1999 05:00:40 GMT
X-Priority: 3
X-MSMail-Priority: Normal
X-Newsreader: Microsoft Outlook Express 5.00.2314.1300
X-MimeOLE: Produced By Microsoft MimeOLE V5.00.2314.1300
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp
Reply-To: djgpp AT delorie DOT com

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;

/* MyRandom()
 * Returns a random number from 0 to 32,767.
 */
int MyRandom(void)
{
  curSeed = (curSeed * 1103515245) + 12345;
  return (curSeed & 0x7fff0000) >> 16;
}

/* SetSeed()
 * Replaces srand(). Sets the seed for MyRandom().
 */
void SetSeed(int seed)
{
  curSeed = seed;
}

/* GetSeed()
 * Returns the current seed for MyRandom() so it can be saved.
 */
int GetSeed(void)
{
  return curSeed;
}

/**** and paste ****/

If you are using Allegro's install_timer(), an initial seed can be obtained
by
    SetSeed(retrace_count);

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,
    SetSeed(myState.randSeed);

> Nate Eldredge
> neldredge AT hmc DOT edu

Damian Yerrick
http://come.to/yerrick


- Raw text -


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