Portable random numbers_(re)

Tue, 28 Oct 1997 20:15:34 GMT



Jesper Lund wrote:

> >IMHO, this code is still broken.  On a non-DOS platform it tries to
> >pass an integer to a function expecting a cardinal and always returns a
> >type check error and doesn't compile.  Is there some advanced switch
> >that should be used in situations like this to turn off type-checking
> >(I've never really looked at all of the directives and didn't see where
> >they were in the docs after a quick look) and the type-casting section
> >is empty.
> >
> >The code in question:
> >FUNCTION Rand:Integer; C;
> >PROCEDURE SRand(Seed:Cardinal); C;
> >FUNCTION GetPID:Integer; C;
> >
> 
> Regarding portability: in my DJGPP C library, there is no `srand' function,
> but a `srandom' function which expects an int (=Integer) as parameter.

Hold it, please! The code fragment you're quoting is inside an $ifdef. It's
not used in DJGPP. Please refer to the complete unit. I posted it here
recently, and I'll make it available under the following URL tomorrow or so:
http://home.pages.de/~fjf/rand.pas

> I don't see how this should emulate the Randomize procedure in Borland
> Pascal.

The DJGPP code does so, and that's all I claimed. On non-Dos systems, it's
almost impossible to emulate BP's behaviour since it depends on the internal
system timer format used by Dos. I now added a comment to the unit making
this clear.

> According the info docs for my C library, getpid returns the unique
> process number for the program running on the system.  If you call getpid
> twice in the same program, you get the same integer.

(* This can happen (with Randomize) in BP, too, if within the same 1/18.2
   seconds interval. Therefore, doing so is not recommended! *)

> Why not compute a seed
> from the system time (somehow), as Borland Pascal does?

If you tell me how to do it, I'd like to! To do it "good", we'd need more
than 1 second resolution, and getting this was the original problem here...

> That is, if we need a Randomize procedure at all?  My C library (DJGPP)
> doesn't have such a beast.  Ok, there is Borland compatibility ....

Well, there must be any way to seed the Random randomly... :-) Otherwise,
the program would produce the same sequence of random numbers in each run.
AFAIK, the usual way to do so in C under Unix is GetPID, therefore I did it
like that. A better general way would be most welcome.

> I use random numbers a great deal in my work (statistical simulations), and
> in most cases (if not all) I specify the seed myself, since this is the only
> way of gettting reproducible results.

There'd be no problem doing so with my unit (though it should not be used
here, because it's only for BP compatibility). OTOH, I'd guess, most game
programmers wouldn't like this behaviour... ;-)

> When I was programming in Turbo Pascal (the pre-GPC era), I *never* used the
> Random function in the system unit because I did not have source code and
> thus couldn't see how the pseudo-random sequence was generated.  There are a
> lot of bad (= non-random) random number generators out there!

True. BTW, my unit uses the same algorithm (though, of course, completely
different code) as BP (7.0 at least). Now you can check if it's a good
algorithm. The main formula is:

 RandSeed := $8088405 * RandSeed + 1 {mod 2^32}

> The following piece of code represents a widely used procedure for generating
> uniform random numbers (reals between 0.0 and 1.0), but it could easily be
> changed to generating integers between 0 and 2^31-1 (just modify the last
> line in RanUni).
> 
> The routine is portable to all systems with a 32-bit integer type.  It does
> not rely on controlled overflow which may not be portable.  The technically
> inclined can consult the reference given in the comments.  BTW, GPC users
> familiar with the book(s) `Numerical Recipes' may be interested in knowing
> that the `ran04 routine in the latest (2nd) edition (C only; not Pascal)
> corresponds to the algorithm below.

Thanks for the code! If it's not copyrighted/patented, we can perhaps use it
for a built-it Random function in GPC.

-- 
Frank Heckenbach, Erlangen, Germany
heckenb@mi.uni-erlangen.de
http://home.pages.de/~fjf/links.htm


Frank Heckenbach (heckenb@mi.uni-erlangen.de)

HTML conversion by Lluís de Yzaguirre i Maura
Institut de Lingüística Aplicada - Universitat "Pompeu Fabra"
e-mail: de_yza@upf.es