[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: Random Number Generator Needed!
In article <1992Nov19.154845.2806@brandonu.ca> marshall@brandonu.ca (Edward S. Marshall) writes:
>Yet another plea from a desperate psuedo-BASIC programmer:
>
>I'm in need of a SHORT and SIMPLE random-number generator for ML. Something
>that'll return a number between $00 and $FF at a variable memory location.
>I've tried using a few locations for a seed, but nothing seems to come up
>random enough. Basically, I'm stuck. Anyone who's got some code that can
>help me out, PLEASE write! If it helps, I'm running on an e//e. Thanks in
>advance!
Counting the time until the VBL signal inverts (bit 7 of $C019) is one
possible way. Works on the GS and IIe at least...not sure about the IIc
or IIc Plus offhand.
The system randomizes $4E and $4F while waiting for the user to hit a key.
You might be able to use that.
If you're looking for a pseudo-random algorithm, here's my favorite. It
cycles through all 255 nonzero values before repeating (just be sure not
to seed it with zero).
lda num
asl a
bcc done
eor #$87
done sta num
Simple, no? Shift it left a bit; if you shifted out a 1 bit, exclusive or
with $87, and the result is also the seed for next time. To jumble the
order better (so you don't get 1, 2, 4, 8, 16, 32, 64, 128 in order, for
example, you can call the whole thing 11 times each time you want a number,
and you still get all 255 values before it repeats).
For 16 bits, the mask is $1D87, and you can repeat 19 times for more jumbled
results (there are longer masks that I don't remember offhand, and in general
the jumble-count is 3 more than the number of bits in your values).
Source: An article in Popular Electronics, probably in the early 80s. I
should dig that up sometime.
--
David A. Lyons, Apple Computer, Inc. | DAL Systems
Apple II System Software Engineer | P.O. Box 875
Internet:dlyons@apple.com | Cupertino, CA 95015-0875
My opinions are my own, not Apple's.