[A86] Re: Random number generator
[A86] Re: Random number generator
There are lots of different methods. The classic method used by most C
libraries is the linear congruential method, as described by Knuth in volume
2 of The Art of Computer Programming. I'm not suggesting that you go out
and get those books, but they definitely have a lot of interesting stuff.
A good page on LCRNG's is at the following URL:
There is plenty of information out there on the technique, as a quick google
search will show you. The following routine is from Matthew Shepcar's
Bomber Bloke. I'm fairly sure it is a linear congruent generator:
; a spiffingly excellent random number generator:
ld a,7921 & 255
ld (Seed),hl ;seed=(seed*7921+1) MOD 65536
It's actually pretty simple if you see through the code (Shepcar has a thing
about writing awesome code that you can never figure out). The comment that
shows what happens to the seed is indeed correct.
It starts out by loading the seed into DE. It starts out at 12345. B is
used as the loop counter, to multiply. It's loaded with $10, or 16. A and
C are used for the multiply constant, 7921. The loop multiplies DE by AC,
with the result in HL. HL is then incremented, as the addition constant is
1. The seed is then saved, so it can be used the next time. The routine
returns the value in HL and in A. The 8 bit value (in A) is the high byte
of the value (as is recommended by Knuth).
If you want a good routine and can get by with about 800-900 t-states (quick
guess of it's speed), then I recommend using it.
I probably should have used that routine when writing Zelda 86, but I
decided to write my own. The routine is probably not very good, but seemed
to work ok. It's an interesting example of just trying stuff until you get
workable results. Don't use it if you need statistically correct numbers,
or even close to perfectly distributed numbers. I don't think it will
return every possible value. Then again, I'm not sure exactly what it does,
and I don't feel like figuring it out :)
; a fast, yet decent random number generator
; returns: a = psuedo random number
; destroyed: flags
It basically does some junk operations against the R register, the seed, and
some "random" byte pulled out of the ROM.
Another approach that should work pretty well is to use a table. Linear
congruential is mathematically proven to be correct, so it's likely better.
But I'm pretty sure a table would be faster and with the right algorithm,
return even distribution and provide a long period. I used this method when
doing Game Boy programming. This is the random number generator used in
The table is evenly distributed with 256 bytes. Looking back at the code,
the last part seems pretty silly. It loads a 16 bit random seed, adds 103
to it and saves the random seed. It then adds the complement of the high
byte with the low byte, and retreives that position in the table as the
random number. I think it would work just fine if only the low byte of the
seed were used. Then again, maybe it doesn't matter.
Amazingly, I even have the C code that generated the table. I cut the
for(i = 0; i < 256; i++)
a[i] = i;
for(i = 0; i < 256 * 16; i++)
swap(&a[rand() % 256], &a[rand() % 256]);
Hopefully, this should answer everyone's questions about random number
> Does anyone know how to generate a random number without using the r
> register? (I wnat to generate several in a row and using the r register
> are almost identical.)