A86: Re: A challenge or something
[Prev][Next][Index][Thread]
A86: Re: A challenge or something
Simple, the code in the ROM does it, so there has to be a way. There are
many different methods. The first thing to realize is that nothing is
truely random, especially in computers. In fact, nothing in the universe is
random. The changes in everything between two periods of time, even
infinitely small, are predictable. However, this does not mean it is easy.
It is not possible for anyone to do this, even on a small scale. Therefore,
you are not generating a random number. That is impossible. You are
generating a number that is not easily predictable. Given this
understanding, the task becomes much easier.
The a good first place to look is at some library code for a language that
has random number generator, such as for the C rand() function. The rand()
function uses the srand() function, which is used to provide it with a
"seed". Remember, that nothing is random, so you have to start somewhere.
This seed is usually generated from the system clock. The seed is
multiplied and added by several very large values, and usually some shifting
is involved. This new value is returned as the random number, and used as
the new seed value, perhaps after some other operations have been performed
on it. A good example of this in z80 is Matthew Shepcar's random routine,
which can be found in the Bomber Bloke source code.
Because multiplying is an expensive operation on processors like the z80,
there are other methods that can be used. A table of pregenerated random
numbers can be saved, and this can be accessed quickly. A counter is kept
specifying the offset at which to retreive the number, and each time the
table is accessed, the counter would be incremented. A table is not always
necessary. The program's or ROM's code can be used for a semi-random table.
Because the calculator does not have a system clock from which to start a
seed, another method must be used. One often used methods is the memory
refresh register, R. This is not always as "random" as is needed,
especially if called in succession, but it is useful for setting the initial
seed for other methods. Another place to get the seed is from the user.
Many programs have a title screen that waits for the user to press a key.
During this time, increment a counter every so often, and use this value for
the seed. If the counter is incremented fast enough, then it should be
different every time. Other techniques would be to sum up the value of all
the memory on ram page (the area starting at $c000), which is likely to be
different each time the program is run.
I am including an example below to help get you started. This routine is
used in Zelda 86 and seems to work quite well:
; a fast, yet decent random number generator
; returns: a = psuedo random number
; destroyed: flags
RandomNumber:
push bc
ld a,r
ld b,a
ld a,($1234)
_@RandomNumberSeed =$-2
ld c,a
ld a,(bc)
ld (_@RandomNumberSeed),a
xor b
sub b
add a,c
pop bc
ret
> Can any of you high god-type asm programmers do something I've been
wondering
> about for years? it's this:
>
> Make an assembly program that contains no rom calls or anything like that
> that will make a random number.
>
> I'm just wondering how people initally did it...
References: