engber@thylacine.CS.WISC.EDU (Mike Engber) (09/20/89)
I'm looking for the details of how Random is implemented in QuickDraw so students of mine that are not working on Macs can generate the same sequence of random numbers to use in testing their programs. I'm hoping it's something like: function Random; begin RandSeed := (RandSeed*c1 + c2) mod c3; Random := RandSeed; end; So I need the values for c1,c2, & c3. Anyone know them or how to figure them out efficiently? -ME
john@trigraph.uucp (John Chew) (09/22/89)
In <8505@spool.cs.wisc.edu> engber@thylacine.CS.WISC.EDU (Mike Engber) writes: >I'm looking for the details of how Random is implemented in QuickDraw >so students of mine that are not working on Macs can generate the same >sequence of random numbers to use in testing their programs. Random() generates random numbers in the range [-32767,32767] using a 32-bit seed stored in the QuickDraw global randSeed. Each time it is called, it does the following: 1. Multiply randSeed by 0x41A7 to form a 47-bit product. 2. Take bits 31 through 46 (MSB) of the product and add them as an unsigned value to bits 0 (LSB) through 30 of the product to form the new seed. 3. Return the low sixteen bits of the product as the random number, except when they are the value 0x8000, in which case return zero. Here is some Lightspeed C code to clarify the above: ----- !#define A ((unsigned long)0x41A7) !#define High(x) ((unsigned int)HiWord((x))) !#define Low(x) ((unsigned int)LoWord((x))) !int myRandom(void); ! !int myRandom() ! { ! unsigned long temp; ! ! /* wish i had 64-bit data... */ ! temp = A*High(randSeed) + High(A*Low(randSeed)); ! randSeed = ((temp & 0x7fff) << 16) + High(temp<<1) + Low(A*randSeed); ! ! if (Low(randSeed) == 0x8000) ! return 0; ! else ! return Low(randSeed); ! } ----- John -- john j. chew, iii phone: +1 416 425 3818 AppleLink: CDA0329 trigraph, inc., toronto, canada {uunet!utai!utcsri,utgpu,utzoo}!trigraph!john dept. of math., u. of toronto poslfit@{utorgpu.bitnet,gpu.utcs.utoronto.ca}