This is a neat little random code I've adapted from 6502 assembly :
;--------------------------------------
RandomNumber ;subroutine
mov R0,#RandomVar
mov a,@R0
clr c
rrc a
jc Neor
xrl a,#0B4h
Neor
mov @R0,a
ret
Where RandomVar is an int. RAM. This generates 0-255 possible numbers and never repeats. Call this code once every frame.
To limit the result is more difficult, you can use ANL to limit the range of A ex.:
anl a,#003h ;limit A to 0-3
anl a,#007h ;limit A to 0-7
anl a,#00Fh ;limit A to 0-15
anl a,#01Fh ; limit A to 0-31
anl a,#03Fh ; limit A to 0-63
anl a,#07Fh ; limit A to 0-127
load the random var to A and use ANL.
mov a,@R0 ;where R0 is the random var (int RAM)
anl a,#00Fh
now A could be 0 up to 15