[comp.sys.mac.programmer] Calling Random

davidh@ronin.cc.umich.edu (David Hotchkiss) (04/21/89)

I'm trying to write some simple simulation programs that involve making
calls to the random number generator.  Whenever I try calling the Random
funtion, I get a Bus error.  However, the stackpointer is still even.
 
I've followed the suggestions in IM to call GetDateTime and set randseed
equal to it but I still have the same problem.

Any help in discovering the source of the problem would be much appreciated.

  //david
  
   
/////////////////////////////////////////////////David Hotchkiss///////////////
\\\\\\\\\\\\\\\\\INTERNET:david_hotchkiss@um.cc.umich.edu\\\\\\\\\\\\\\\\\\\\\\
//////////////////////////davidh@ronin.us.cc.umich.edu/////////////////////////
\\\\\\\\\\BITNET:USERW264@UMICHUM\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
UUCP:davidh@ronin.UUCP////////////////{insert irrelevant saying here}//////////
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

darin@Apple.COM (Darin Adler) (04/22/89)

In article <1055@mailrus.cc.umich.edu> davidh@ronin.cc.umich.edu (David Hotchkiss) writes:
> 
> I'm trying to write some simple simulation programs that involve making
> calls to the random number generator.  Whenever I try calling the Random
> funtion, I get a Bus error.  However, the stackpointer is still even.

Random is a QuickDraw routine, so you must have an A5 world set up for
QuickDraw globals, and call InitGraf before calling Random.

This was a bug in the old "Insect" desk accessory by Burt Sloane. He used
Random to choose a random place for the insect to climb down the screen,
but didn't have QuickDraw globals in his insect code. He found the bug, but
not before copies of the DA spread around most of the Mac community.
-- 
Darin Adler, System Software Scapegoat, Apple Computer
	Internet: darin@Apple.com
	UUCP: {nsc,sun}!apple!darin

waldman@endor.harvard.edu (benjamin Waldman) (04/22/89)

In article <1055@mailrus.cc.umich.edu> davidh@ronin.cc.umich.edu (David Hotchkiss) writes:
>
>I'm trying to write some simple simulation programs that involve making
>calls to the random number generator.  Whenever I try calling the Random
>funtion, I get a Bus error.  However, the stackpointer is still even.
> 
>I've followed the suggestions in IM to call GetDateTime and set randseed
>equal to it but I still have the same problem.

Have you initilized QuickDraw yet?  Random() uses the value of randSeed
to determine the next random number, and randSeed is a QuickDraw global,
not a system global.  Thus, if QuickDraw hasn't been initialized, and
A5 or 0(a5) (I forget which) is pointing to and odd address (on a 68000
machine) or a huge address (68020), you'll bomb.

Ben Waldman
Harvard College

waldman@endor.harvard.edu

ephraim@think.COM (Ephraim Vishniac) (05/06/89)

In article <1499@internal.Apple.COM> darin@Apple.COM (Darin Adler) writes:
>Random is a QuickDraw routine, so you must have an A5 world set up for
>QuickDraw globals, and call InitGraf before calling Random.

>This was a bug in the old "Insect" desk accessory by Burt Sloane. He used
>Random to choose a random place for the insect to climb down the screen,
>but didn't have QuickDraw globals in his insect code. He found the bug, but
>not before copies of the DA spread around most of the Mac community.

A much improved version of the Insect DA (improved by Yours Truly),
complete with source code, is available via ftp from the info-mac
directory at sumex-aim.stanfored.edu.  It fixes this bug and many
others.

I'll happily mail a copy to anybody who can't get it from sumex.  It
still loses on the Mac II in multi-colors, but at least it's
compatible with third-party monitors, and has an option to kill off
insects.  It's also an interesting example of how to install and
(ab)use VBL tasks.

Ephraim Vishniac  /  Internet: ephraim@think.com  / AppleLink: ThinkingCorp
Thinking Machines Corporation / 245 First Street / Cambridge, MA 02142-1214

	"Arlo Guthrie, it seems, has found what he was looking for:
		God, and the Macintosh." (Boston Globe)