Well, here's my random number generating routine that I use:
Code:
System.Main.Random.16:
;Generates random 16 bit number in Random.Random0 and Random.Random1.
;Before using, initialize these values with any number, preferably base it off of time it takes
;for user interaction at some location. Then initialize Random.Index0 and Random.Index1 with any
;4-bit value (It's a good idea to use Random0 AND #$0F and Random1 AND #$0F as initialization values).
;In order to generate a random number between 2 specified values X and Y, where Z = Y - X, Generate
;a random number without bounds, divide it by Z, and either subtract the remainder from Y or add it to X.
;For example, if one needs a random value between 2 and 6, one may generate the 16 bit random value $E351.
;Take this value divided by 4 ($38D4 remainder 1), and add the remainder (1) to 2, or subtract it from 6, either
;making the final result either 3 or 5.
ldx System.Main.Random.Index1 ;3
clc ;5
lda System.Main.Random.Random0 ;8
adc System.Main.Random.Random1 ;11
eor System.Main.Random.TableB.w,x ;15
sta System.Main.Random.Random1 ;18
lsr System.Main.Random.Random0 ;23
inx ;25
txa ;27
and #$0F ;29
sta System.Main.Random.Index1 ;32
ldx System.Main.Random.Index0 ;35
lda System.Main.Random.Random0 ;38
eor System.Main.Random.TableA.w,x ;42
clc ;44
adc System.NMI.VBLCount ;47
sta System.Main.Random.Random0 ;50
lda System.Main.Random.Random1 ;53
adc #0 ;55
sta System.Main.Random.Random1 ;58
inx ;60
txa ;62
and #$0F ;64
sta System.Main.Random.Index0 ;67
rts
System.Main.Random.TableA:
.db $4A,$C3,$E4,$BD,$11,$07,$F5,$26,$D2,$30,$58,$69,$7B,$AF,$9E,$7C
System.Main.Random.TableB:
.db $38,$A2,$AF,$C9,$20,$F3,$67,$23,$59,$10,$50,$4D,$85,$BA,$41,$E7
That's copied and pasted from one of my projects, and I'm pretty sure the comments explain everything. The System.NMI.VBLCount variable is just a variable that's incremented by one every frame. What I usually do is before I work with the random number generator, I keep adding a value every frame to Random0 and Random1 and stop doing so when the user presses a button. This gives you a semi-random number to use as a seed (Also somehow come up with a 4-bit seed value for Index0 and Index1). Then you can see the code basically takes those values and distorts them by EORing them with values in the tables listed and adding the VBLCount variable to them. You end up with a random-ish value between 0 to 65535 stored with the low 8 bits in Random0, and the high 8 bits in Random1. If you're looking for a random number in a certain range, there are a couple things you can do using the value generated by this routine, and I explain those in the comments. I don't know if these is the usual kind of way to generate random numbers, but it works well for me. I use it for a video keno clone (a game like the lottery that draws random numbers between 1 and 80), and I haven't seen the same set of numbers drawn twice.