Path: utzoo!utgpu!jarvis.csri.toronto.edu!mailrus!ames!pasteur!ucbvax!decwrl!labrea!polya!kaufman From: kaufman@polya.Stanford.EDU (Marc T. Kaufman) Newsgroups: comp.sys.mac.programmer Subject: Re: Random help, please Message-ID: <6616@polya.Stanford.EDU> Date: 3 Feb 89 16:35:26 GMT References: <69823DN5@PSUVM> Reply-To: kaufman@polya.Stanford.EDU (Marc T. Kaufman) Organization: Stanford University Lines: 93 In article <69823DN5@PSUVM> DN5@PSUVM.BITNET writes: >Hi netlanders: > I am working on a DA which needs some random numbers... > I am working on a Mac II under Multifinder. This is a nasty problem, >because everything else works. >ps. > I am using MPW 3.0 B something. Have I got a deal for you. The function below will do what you want. I wrote it as an exercise to see just how fast a '020 will go: Marc Kaufman (kaufman@polya.stanford.edu) --------------------- CUT HERE, submit to MPW Assembler ---------------------- ******************************************************************* * ROUTINE Random.a * FUNCTION Provide Minimal-Standard random number generator * CALLING C-compatible calling sequences * * This generator is taken from CACM, October 1988, p.1192 * * It was designed by Stephen K. Park and Keith W. Miller * * For future reference: Possible better multipliers are 48271 and 69621 ******************************************************************* CASE ON MACHINE MC68020 PROC ENTRY MSRandomSeed Start DC.W ' ) Written by Marc Kaufman, Kaufman Research, 1988 ' MSRandomSeed DC.L 1 ; Place to store seed value ENDPROC ******************************************************************* * ROUTINE MSGetSeed * FUNCTION Return the current seed value * INPUT none * OUTPUT D0 = the seed ******************************************************************* MSGetSeed FUNC EXPORT move.l MSRandomSeed,D0 rts ENDFUNC ******************************************************************* * ROUTINE MSRandom * FUNCTION Return the next random number * INPUT none * OUTPUT D0 = the number ******************************************************************* MSRandom FUNC EXPORT lea MSRandomSeed,A0 move.l (A0),D1 mulu.l #16807,D0:D1 ; long multiply divu.l #$7fffffff,D0:D1 ; modulo 2^31 -1 move.l D0,(A0) rts ENDFUNC ******************************************************************* * ROUTINE MSRanSet * FUNCTION Set the random number seed * INPUT The seed value * OUTPUT The seed value ******************************************************************* MSRanSet FUNC EXPORT move.l 4(A7),D0 ; the value the user wants to use and.l #$7fffffff,D0 beq.s MSGetSeed ; zero is not a valid value cmp.l #$7fffffff,D0 beq.s MSGetSeed ; neither is 2^31-1 lea MSRandomSeed,A0 move.l D0,(A0) rts ENDFUNC END