Newsgroups: comp.lang.pascal Path: utzoo!utgpu!watserv1!maytag!watstat.waterloo.edu!dmurdoch From: dmurdoch@watstat.waterloo.edu (Duncan Murdoch) Subject: Re: TP 6.0 Random Number Generator Message-ID: <1991Jan15.163811.15107@maytag.waterloo.edu> Sender: daemon@maytag.waterloo.edu (Admin) Organization: University of Waterloo References: <1991Jan14.213834.26624@maytag.waterloo.edu> Date: Tue, 15 Jan 91 16:38:11 GMT Lines: 24 In article <1991Jan14.213834.26624@maytag.waterloo.edu> I wrote: > >3. The extended (floating point) version of random is calculated by dividing > the absolute value of the seed by 2^31, after updating it. > >This has a really nasty consequence. If you choose to calculate your own >integer valued random numbers by the formula > > myrandom(n) := trunc(n*random); > >you'll have random coming out to 1 once in 2^32 tries (it's the value >you'll get first if you start with randseed = -1498392781), and so >myrandom(n) will give n. If it's indexing into an array[0..n-1], this >will give an out of bounds error. I've now taken a look at the real version of random (which you get if you compile with the $N- option), and find that it doesn't suffer from this flaw. It appears to calculate the value by dividing an unsigned long version of randseed by 2^32; this means it can never return 1. It does return 0 if the seed is 0 after updating; you can get this by setting randseed to 649090867. Duncan Murdoch dmurdoch@watstat.waterloo.edu