[comp.sys.mac] How Doy you get a real random number in LSC?

jasst3@cisunx.UUCP (Jeffrey A. Sullivan) (03/11/88)

How do you get a real random number in LSC?  I have tried using rand() and
Random() (from math lib and quickdraw, repectively).  They both give the same
sequence of numbers each time at startup.  I am trying to write a randomizer
INIT that works on a mac II, but the randomness is all that's holding me
back.  (I think...)


-- 
..........................................................................
Jeffrey Sullivan			  | University of Pittsburgh
jas@cadre.dsl.pittsburgh.edu		  | Intelligent Systems Studies Program
jasper@PittVMS.BITNET, jasst3@cisunx.UUCP | Graduate Student

t-jacobs@utah-cs.UUCP (Tony Jacobs) (03/12/88)

Try using the tick count as a seed or some portion of the tick count. It is
always changing.

-- 
Tony Jacobs * Center for Engineering Design * U of U * t-jacobs@ced.utah.edu

jv0l+@andrew.cmu.edu (Justin Chris Vallon) (03/15/88)

jasst3@cisunx.UUCP (Jeffry A. Sullivan) asks:
>>
How do you get a real random number in LSC?  I have tried using rand() and
Random() (from math lib and quickdraw, repectively).  They both give the same
sequence of numbers each time at startup.  I am trying to write a randomizer
INIT that works on a mac II, but the randomness is all that's holding me back.
(I think...)
<<

The QD function Random() uses the global variable RandSeed, which is always
initialized to the same number when you call InitGraf(&thePort).  To change the
sequence of random numbers, you must initialize the seed.  A seed that I use a
lot is either the current system time, since it will never be the same for two
instances of your initialization.  Look for the function which returns the
number of seconds elapsed since 1/1/04.  Use this number (ANDed w/0xffff) to
generate a unique random number seed.

-Justin
justin.vallon@andrew.cmu.edu

hallett@lear.steinmetz (Jeff A. Hallett) (03/17/88)

In article <7671@cisunx.UUCP> jasst3@cisunx.UUCP (Jeffrey A. Sullivan) writes:
>How do you get a real random number in LSC?  I have tried using rand() and
>Random() (from math lib and quickdraw, repectively).  They both give the same
>sequence of numbers each time at startup.  I am trying to write a randomizer
>INIT that works on a mac II, but the randomness is all that's holding me
>back.  (I think...)

Well, rand() works pretty well if you remember to change the seed each
time.  The best way I've seen to do this is by using the value off the
System clock.  You have to take it modulo the max seed size and then
set the seed to this value.  This will keep you from getting the same
"random" sequence every time.

Jeffrey A. Hallett                     | ARPA: hallett@ge-crd.arpa   
Software Technology Program    	       | UUCP: desdemona!hallett@steinmetz.uucp
GE Corporate Research and Development  | (518) 387-5654
+--------------------------------------+--------------------------------------+
|                            Credo Quia Absurdum Est                          |
+-----------------------------------------------------------------------------+

olson@endor.harvard.edu (Eric K. Olson) (03/17/88)

In a recent article Jeffrey A. Hallett writes:
>In article <7671@cisunx.UUCP> jasst3@cisunx.UUCP (Jeffrey A. Sullivan) writes:
>>How do you get a real random number in LSC?  I have tried using rand() and
>>Random() (from math lib and quickdraw, repectively).  They both give the same
>>sequence of numbers each time at startup.  I am trying to write a randomizer
>>INIT that works on a mac II, but the randomness is all that's holding me
>>back.  (I think...)
>
>Well, rand() works pretty well if you remember to change the seed each
>time.  The best way I've seen to do this is by using the value off the
>System clock.  You have to take it modulo the max seed size and then
>set the seed to this value.  This will keep you from getting the same
>"random" sequence every time.

There is a similar seed value for Random(), called randSeed.  It's one
of those quickdraw globals, like thePort and screenBits.  It's a long,
so it can take the value of the system clock directly.

It is initialized to 1 by InitGraf, and if you don't reinitilize it, you
will, indeed, get the same sequence every time.

-Eric
	    "We're writing tomorrow's software yesterday."
Eric K. Olson     olson@endor.harvard.edu     harvard!endor!olson     D0760
   (Name)                (ArpaNet)                 (UseNet)        (AppleLink)