www.delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/2000/03/09/12:08:27

From: buers AT gmx DOT de (Dieter Buerssner)
Newsgroups: comp.os.msdos.djgpp
Subject: Re: Random Numbers
Date: 9 Mar 2000 13:33:14 GMT
Lines: 33
Message-ID: <8a896n$3aa3v$1@fu-berlin.de>
References: <8a6fpg$gjg$1 AT news8 DOT svr DOT pol DOT co DOT uk>
NNTP-Posting-Host: pec-0-225.tnt1.s2.uunet.de (149.225.0.225)
Mime-Version: 1.0
X-Trace: fu-berlin.de 952608794 3483775 149.225.0.225 (16 [17104])
X-Posting-Agent: Hamster/1.3.13.0
User-Agent: Xnews/03.02.04
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp
Reply-To: djgpp AT delorie DOT com

Ben Davis wrote:

>I know what random() and srandom() are. But what are initstate() and
>setstate()? The help files are very unspecific. I'd be grateful if anyone
>could explain them to me.

random() works like this:
  return r[n] = (r[n-l] + r[n-s]) >> 1; /* nth random number */

So you have to store the max(l,s) last random numbers in
an array used as a cyclic buffer. In the default mode (without
using initstate) l and s are 31 and 3. When you call initstate,
you give a buffer for the state information and the function
will look, what are the largest suitable l and s to fit in
your buffer. It will from then on work with your buffer as its
state array (also 2 pointers needed for the cyclic buffer and
some other internal information will be stored in this buffer).

If you stop some simulation, that you want to continue at
an other time, you could store your buffer to disk, and
at this other time, you could use setstate() to initialize
random() to exactly that state, were you stopped.

If you have the libsrc installed, you can find out more 
in src/libc/compat/stdlib/random.c

BTW. I would not suggest random() for seriuos simulations. 
The constanst l and s must be properly choosen. 
I have articles with lists of suggested values for l and s, the values in 
hardcoded in random() are not on these lists.

Regards,
Dieter

- Raw text -


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