Path: utzoo!news-server.csri.toronto.edu!cs.utexas.edu!uunet!mcsun!hp4nl!eurtrx!euraiv1!reino From: reino@cs.eur.nl (Reino de Boer) Newsgroups: comp.lang.pascal Subject: Re: Random numbers Message-ID: <1991Mar8.110210.22323@cs.eur.nl> Date: 8 Mar 91 11:02:10 GMT References: <1991Mar7.193727.1629@tjhsst.vak12ed.edu> <1991Mar8.031013.14902@casbah.acns.nwu.edu> Reply-To: reino@cs.eur.nl Organization: Erasmus University Rotterdam Lines: 81 In <1991Mar8.031013.14902@casbah.acns.nwu.edu> ajburke@casbah.acns.nwu.edu (Austin Burke) writes: >In article 1624 John Mitchell writes: >> How does TP 5.5 generate random numbers? > ...... > There is no internal list. The formula used is, approximately, the >following: > seed = (multiplier * seed) + (incrementor mod modulus) This probably is: seed = ((multiplier * seed) + incrementor) mod modulus where "modulus == computer word size" to simplify the calculation. >>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? Check out Volume 2 of Donald E. Knuth -- The Art of Computer Programming The first half of this book is entirely dedicated to pseudo random numbers and their generation. Following are some routines I recently used to check some properties of prime numbers (this is sun-pascal, although I think it's even level 0): function random1 : float; const a = 1664525.0; c = 1.0; m = 4294967296.0; begin seed1 := modulo( seed1 * a + c, m ); random1 := seed1 / m end; { random1 } function randint1( max : int ) : int; begin randint1 := floor( random1 * max ) end; { randint1 } function random2 : float; const a = 17059465.0; c = 1.0; m = 34359738368.0; begin seed2 := modulo( seed2 * a + c, m ); random2 := seed2 / m end; { random2 } function randint2( max : int ) : int; begin randint2 := floor( random2 * max ) end; { randint2 } function randnumber( digits : integer ) : int; var result : int; i : integer; begin result := floor( random( 9.0 ) * 9.0 + 1.0 ); for i := 2 to digits do result := 10.0 * result + floor( random( 10.0 ) * 10.0 ); randnumber := result end; { randnumber } If needed, I can look up the implementation of modulo() and floor() for you, although they are fairly trivial. Hope this helps -- Reino -- Reino R. A. de Boer "We want to build the right product right, right?" Erasmus University Rotterdam ( Informatica ) e-mail: reino@cs.eur.nl