[comp.sys.mac.programmer] Random number generator - Think Pascal - help

burchil@ccu.umanitoba.ca (Charles Andrew Burchill) (05/25/91)

Would someone send me an example of a random number generator in
Think Pascal.  Using the random function always returns the same
number, I would like to know how to properly set the rand(seed)
using the time.

I realize that this is probably a very easy question but please mail
me an answer anyway.

Thanks in advance
Charles
<Burchil@ccu.umanitoba.ca>

Jahnke@brahms.biosci.arizona.edu (Jerome Jahnke) (05/30/91)

In article <1991May24.201417.1423@ccu.umanitoba.ca> burchil@ccu.umanitoba.ca
(Charles Andrew Burchill) writes:
>
>Would someone send me an example of a random number generator in
>Think Pascal.

I've included how I get a random number, as well as a pseudo random number
generator. I was not sure of what you wanted the function myRandom is a pseudo
random number generator. The numbers it generates of close to Apples (given the
same seed).

The other part is what I do to get a "useable" random number (i.e. scaling the
raw number). Getting a random number that can be either postive or negative is
an exercise I leave to you.  

		function random (max: integer): integer;
			var
				tempLong: longint;
				tempint: integer;

			function myRandom: integer;
				var
					temp1, temp2, temp3: longint;
			begin
				temp1 := BAnd(Seed, $0000FFFF) * $000041A7;
				temp3 := BSR(Seed, 16) * $000041A7 + BSR(temp1, 16);
				temp2 := BSR(temp3 * 2, 16);
				temp1 := BAnd(temp1, $0000FFFF) - $7FFFFFFF;
				temp3 := BAnd(temp3, $7FFF);
				temp1 := temp1 + Bor(BSL(temp3, 16), BSR(temp3, 16)) + temp2;
				if temp1 < 0 then
					temp1 := temp1 + $7FFFFFFF;
				Seed := temp1;
				if loword(temp1) = $8000 then
					myRandom := 0
				else
					myRandom := loword(temp1);
			end;

		begin
			tempLong := abs(myRandom);               {Need a positive number}
			tempint := (tempLong * max) div 32768;			{This Scales number to between 0
and max - 1}
			random := tempint + 1;                   {Return the number}
		end;

>Using the random function always returns the same
>number, I would like to know how to properly set the rand(seed)
>using the time.

GetDateTime(RndSeed);

GetDateTime is a procedure which is in IM II (pp 378 in my copy). All it does
it gets the number of seconds since 1/1/04.

Jer,
----
Jerome Jahnke
University of Arizona
Dept. of Molecular and Cellular Biology
'jahnke@joplin.biosici.arizona.edu' or (602) 621-3820

spud@Apple.COM (Dave "Spud" Kalin) (06/03/91)

In article <1991May24.201417.1423@ccu.umanitoba.ca> burchil@ccu.umanitoba.ca (Charles Andrew Burchill) writes:
>
>Would someone send me an example of a random number generator in
>Think Pascal.  Using the random function always returns the same
>number, I would like to know how to properly set the rand(seed)
>using the time.
>
 
I was just pondering this question last week, so here's what I found out:
the _Random call apparently does not get seeded from the RndSeed low-memory
global, but from the grafport global RandSeed. Whenever you call InitGraf,
the global is reset to 1, hence your repeating random numbers. 

A fix to this problem is as easy as this: 

  randseed := tickcount;

every time you try a new random sample..  Hope this helps!


-- 
---------------------------------------

Dave "Spud" Kalin
Apple Computer, Inc.

----------------------------------------

?SYNTAX ERROR      (beep!)