Random number generator:
Random:
; See "linear-congruential random number generator" for more.
; rand = (rand * 5 + 0x3611) & 0xffff;
; return (rand >> 8) & 0xff;
lda rand_h ; multiply by 5
sta rand_temp
lda rand_l
asl a ; rand = rand * 4 + rand
rol rand_temp
asl a
rol rand_temp
clc
adc rand_l
pha
lda rand_temp
adc rand_h
sta rand_h
pla ; rand = rand + 0x3611
clc
adc #$11
sta rand_l
lda rand_h
adc #$36
sta rand_h
rts ; return high 8 bits
You need the variables rand_temp, rand_h, and rand_l to be somewhere in ram. The sequence will always be the same with a given seed, so usually games will do something like run a counter and use that as a seed when the player hits the first button.