[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: Apple II Random Events?
Rubywand wrote:
>
> Gabriel Morales writes ...
> >
> > I was wondering:
> >
> > How does an Apple II, say using the BASIC RND command, determine a
> > random number?
> >
<stuff deleted>
> A pretty good way to mix things up is to place code which calls the
> RND() function inside some subroutine which gets frequently accessed and
> where the the number of RND() calls is not predictable. For instance,
> you could place ZZ= RND(1) inside a Get Key routine ...
>
> 50 REM GET A KEY (RETURNS ASCII CODE IN AC)
> 60 ZZ= RND(1): ZZ= PEEK(49152): IF ZZ<128 THEN 60
> 70 AC= ZZ-128: ZZ= PEEK(49168): RETURN
>
> In the above, the number of times ZZ= RND(1) is executed depends
> upon how long the player takes to press a key, say, to answer Yes or No
> to some question. Thousands of RND(1) values may be generated each time
> the player is deciding something and, so, the results of other RND(1)
> uses later in the game become unpredictable.
And since a single unpredictable seeding is as good as many, you
need only do the above trick once, at the beginning of the game--
say, when asking the player their name or about some preference.
Interestingly, the Apple is _always_ generating a 16-bit random seed
whenever it is waiting on a keypress. So if 16 bits (65 thousand) is
enough different scenario starts, you can just peek the two bytes of
the keyboard "random number" and poke them into the Applesoft random
number fraction. I'd recommend the high bytes, but who's counting...
:-)
-michael