[comp.sys.mac.programmer] Random help, please

DN5@PSUVM.BITNET (02/03/89)

Hi netlanders:

  I am working on a DA which needs some random numbers.  My problem is that
I always get the same sequence.  I have tried setting randSeed, but this
doesn't seem to do anything (actually I set it before I call Random, then
save this value, then reset the original value, so that it won't mess up
an application).  However, anything I do (including attempting a FOR loop
repeating for a somewhat random time (GetTimeDate + CountTicks), seems to
do nothing: I still get the same sequence.

  I am working on a Mac II under Multifinder.  This is a nasty problem,
because everything else works.

  Another problem: How do I submit something to comp.sources.mac?  I would
like to submit this DA whenever it is finished.

                               Jay, etc... (D. Jay Newman)
ps.
  I am using MPW 3.0 B something.

kish@.ucalgary.ca (Bradley John Kish) (02/04/89)

In article <69823DN5@PSUVM> DN5@PSUVM.BITNET writes:
>Hi netlanders:
>
>  I am working on a DA which needs some random numbers.  My problem is that
>I always get the same sequence.  I have tried setting randSeed, but this
>doesn't seem to do anything (actually I set it before I call Random, then
>save this value, then reset the original value, so that it won't mess up
>an application).  However, anything I do (including attempting a FOR loop
>repeating for a somewhat random time (GetTimeDate + CountTicks), seems to
>do nothing: I still get the same sequence.

The problem has to do with the way the Mac handles DA's and the Random 
number seed.  The random number seed is stored as a GLOBAL and DA's aren't
allowed to touch with them.  I can't remember what what nasty things happen
if you do mess with them, though.  To get around this problem you have to
write your own random number generator.  Any good book on computer algorithms
will have one.  To start off randomly, set your seed to the system clock.

Hope this was of some help.  Good Luck!


Brad Kish.

***
What's all this disclaimer jazz anyway?

kish@enel.UCalgary.CA
 

kaufman@polya.Stanford.EDU (Marc T. Kaufman) (02/04/89)

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

jkjl@munnari.oz (John Lim) (02/06/89)

In article <69823DN5@PSUVM> DN5@PSUVM.BITNET writes:
>Hi netlanders:
>
>  I am working on a DA which needs some random numbers.  My problem is that
>I always get the same sequence.  I have tried setting randSeed, but this

	You just cannot simply reset randSeed in C by :

		randSeed = TickCount();

	because LSC thinks that randSeed is a DA global offset by A4 and
	not A5 (which is what u want).

	What you need to do is use some assembly to access randSeed. Sorry
	but i dont have my notes and docs here so i cant show it to you
	from memory.

>doesn't seem to do anything (actually I set it before I call Random, then
>save this value, then reset the original value, so that it won't mess up
>an application).  However, anything I do (including attempting a FOR loop
>repeating for a somewhat random time (GetTimeDate + CountTicks), seems to
>do nothing: I still get the same sequence.


	Why dont you use your own random number generator ?

	john

lsr@Apple.COM (Larry Rosenstein) (02/07/89)

In article <2671@munnari.oz> jkjl@munnari.UUCP (John Lim) writes:
>In article <69823DN5@PSUVM> DN5@PSUVM.BITNET writes:

>>  I am working on a DA which needs some random numbers.  My problem is that
>>I always get the same sequence.  I have tried setting randSeed, but this
>
>	You just cannot simply reset randSeed in C by :
>
>		randSeed = TickCount();
>
>	because LSC thinks that randSeed is a DA global offset by A4 and
>	not A5 (which is what u want).

randSeed happens to be a Quickdraw global variable.  A5 always points to a
pointer to the Quickdraw variables.  I think assembly code such as:

	MOVE.L	(A5),A0
	MOVE.L	randSeed(A0),D0

will get randSeed.  From the MPW assembler interfaces, it looks like
randSeed is -$7E.  Looking at the code for _Random in Macsbug seems to
confirm this.


-- 
		 Larry Rosenstein,  Object Specialist
 Apple Computer, Inc.  20525 Mariani Ave, MS 46-B  Cupertino, CA 95014
	    AppleLink:Rosenstein1    domain:lsr@Apple.COM
		UUCP:{sun,voder,nsc,decwrl}!apple!lsr