Path: utzoo!utgpu!news-server.csri.toronto.edu!rpi!think.com!zaphod.mps.ohio-state.edu!swrinde!elroy.jpl.nasa.gov!ncar!noao!amethyst!Jahnke@brahms.biosci.arizona.edu From: Jahnke@brahms.biosci.arizona.edu (Jerome Jahnke) Newsgroups: comp.sys.mac.programmer Subject: Re: Random number generator - Think Pascal - help Message-ID: <2712@amethyst.math.arizona.edu> Date: 29 May 91 20:19:12 GMT References: <1991May24.201417.1423@ccu.umanitoba.ca> Sender: news@amethyst.math.arizona.edu Organization: Dept. Molecular and Cellular Biology: University Lines: 61 In article <1991May24.201417.1423@ccu.umanitoba.ca> burchil@ccu.umanitoba.ca (Charles Andrew Burchill) writes: > >Would someone send me an example of a random number generator in >Think Pascal. I've included how I get a random number, as well as a pseudo random number generator. I was not sure of what you wanted the function myRandom is a pseudo random number generator. The numbers it generates of close to Apples (given the same seed). The other part is what I do to get a "useable" random number (i.e. scaling the raw number). Getting a random number that can be either postive or negative is an exercise I leave to you. function random (max: integer): integer; var tempLong: longint; tempint: integer; function myRandom: integer; var temp1, temp2, temp3: longint; begin temp1 := BAnd(Seed, $0000FFFF) * $000041A7; temp3 := BSR(Seed, 16) * $000041A7 + BSR(temp1, 16); temp2 := BSR(temp3 * 2, 16); temp1 := BAnd(temp1, $0000FFFF) - $7FFFFFFF; temp3 := BAnd(temp3, $7FFF); temp1 := temp1 + Bor(BSL(temp3, 16), BSR(temp3, 16)) + temp2; if temp1 < 0 then temp1 := temp1 + $7FFFFFFF; Seed := temp1; if loword(temp1) = $8000 then myRandom := 0 else myRandom := loword(temp1); end; begin tempLong := abs(myRandom); {Need a positive number} tempint := (tempLong * max) div 32768; {This Scales number to between 0 and max - 1} random := tempint + 1; {Return the number} end; >Using the random function always returns the same >number, I would like to know how to properly set the rand(seed) >using the time. GetDateTime(RndSeed); GetDateTime is a procedure which is in IM II (pp 378 in my copy). All it does it gets the number of seconds since 1/1/04. Jer, ---- Jerome Jahnke University of Arizona Dept. of Molecular and Cellular Biology 'jahnke@joplin.biosici.arizona.edu' or (602) 621-3820