Warning: Probable massive overkill here, but, I'll give a full explanation, then provide some code to do it. Eventually I should put this up more fully somewhere, I imagine...
My "Galaxy Patrol" game needs to randomly generate its map, so I had to make a full-blown random number generator. One of the simplest forms is a "Linear Congruential Generator" or LCG. This starts with some seed value T, then advances itself by computing
T = (a*T + b) MOD c
a, b, and c generally have to be rather large, and have special properties, so you need a full 32-bit multiplier and a bit of research. Fortunately, http://www.6502.org provides basic code for a 32-bit multiplier, and a little web research popped up a lot of generators. I chose the BCPL generator because c takes the value 2^32, and computing MOD 2^32 is really easy. (As an added bonus, its seed value is zero.)
The code for the BCPL LCG random number generator is as follows.
.byte $6d, $83, $10, $90
.byte $31, $1d, $a0, $2a
; PROD: 8 bytes. Holds 64-bit result.
; MULND: 4 bytes. 32-bit multiplicand.
; MULR: 4 bytes. 32-bit multiplier.
; 32 bit multiply with 64 bit product
mult32: lda #$00
sta PROD+4 ;Clear upper half of
sta PROD+5 ;product
ldx #$20 ;Set binary count to 32
* lsr MULR+3 ;Shift multiplyer right
bcc + ;Go rotate right if c = 0
lda PROD+4 ;Get upper half of product
clc ; and add multiplicand to
adc MULND ; it
* ror ; Rotate partial product
sta PROD+7 ; right
dex ; Decrement bit count and
bne -- ; loop until 32 bits are done
To get random bytes, read from the "random" array. (random, prod, mulnd, and mulr are defined elsewhere in the source because the version of the assembler I had at the time needed RAM variables declared in a special section.)