Path: utzoo!news-server.csri.toronto.edu!cs.utexas.edu!usc!wuarchive!udel!haven!adm!news From: CDCKAB%EMUVM1.BITNET@cunyvm.cuny.edu ( Karl Brendel) Newsgroups: comp.lang.pascal Subject: Re: Random numbers Message-ID: <26218@adm.brl.mil> Date: 8 Mar 91 15:23:00 GMT Sender: news@adm.brl.mil Lines: 90 In article <1991Mar8.031013.14902@casbah.acns.nwu.edu>, ajburke@casbah.acns.nwu.edu (Austin Burke) wrote: >In article 1624 John Mitchell writes: > >> How does TP 5.5 generate random numbers? > > The numbers generated are not really random. Each number is >derived from the preceding "random" number by a complex math >equation, and then forced (by another formula) to fit into the given >range. > >> We are using this statement: r := random; which returns a >>real number in the range [0,1). Does TP use some kind of formula >>to generate these values? Or is there an internal list of random >>numbers that is accessed? Does "randomize" seed a formula or >>find a spot in the list to start from? > > There is no internal list. The formula used is, >approximately, the following: > > seed = (multiplier * seed) + (incrementor mod modulus) > > where "seed" is the previous number generated, "multiplier" >is some very large constant number, as is "increment," and "modulus" >is 65536. The result of this formula is then forced to fit into the >range given by the user. > The "randomize" command gives TP its first "seed." This >first seed is derived (I think) from the current time, measured in >hundreths of a second (or even more exact - I'm not sure). Specifics of the random number generator have been discussed at length in the list by Duncan Murdoch . If he doesn't respond directly to this question, email to him or a check of the archives would be appropriate. See particularly articles <1991Jan14.213834.26624@maytag.waterloo.edu> and <1991Jan15.163811.15107@maytag.waterloo.edu>. (Yes, Duncan, I keep a _lot_ of your stuff. :) ) > Through this process, seemingly random numbers are >generated. It is conceivable, but not likely, that the exact same >sequence of "random" numbers may be generated if the initial seed is >the same. This is why the current time is used - it makes this >possiblity all the more unlikely. See the documentation for Randomize. A seed value can be _assigned_. Reuse of a seed value _will_ result in 'the exact same sequence of "random" numbers" being generated. > >> My students are writing a program to approximate pi using the >>Monte Carlo method. Our problem is that we are experiencing a >>relatively wide range of answers for pi (+/- 0.2) which is not >>improved by running the program with more iterations. I wonder if the source of the problem is really in the random number generator itself. Would you like to throw out some code for us to look at? :) > > Sorry, I'm not familiar with the Monte Carlo method. > >>P.S. Does TP generate random integers using the same method? >>P.P.S. Would anyone be able to send me a formula for generating >>good random numbers that might be substituted for the TP function >>call? This routine is based on "Abramowitz & Stegun (1965)", implemented in code received from a statistician whose name I have misplaced. You should be able to modify it for real numbers as well. var seed : longint; function randomInteger(least, most : integer):integer; begin seed := (seed * 23) mod (100000001); randomInteger := least + round((most - least)*seed/100000000); end; {of randomInteger} +--------------------------------------------------------------------+ | Karl Brendel Centers for Disease Control | | Internet: CDCKAB@EMUVM1.BITNET Epidemiology Program Office | | Bitnet: CDCKAB@EMUVM1 Atlanta, GA, USA | | Home of Epi Info 5.0 | +--------------------------------------------------------------------+