RANDB
         PLA
         STA   RETADR
         PLA
         STA   RETADR+1
         PLA
         STA   :NEWMAX
         PLA
         STA   :NEWMIN
         LDA   #1
         STA   :OLDMIN
         LDA   #255
         STA   :OLDMAX
         LDX   #8
         LDA   RNDL+0
:AA
         ASL
         ROL   RNDL+1
         BCC   :BB
         EOR   #$2D       
:BB
         DEX
         BNE   :AA
         STA   RNDL+0
         CMP   #0
         STA   :OLDVAL
         LDA   :OLDMAX
         SEC
         SBC   :OLDMIN
         STA   :OLDRANGE
         LDA   :NEWMAX
         SEC
         SBC   :NEWMIN
         STA   :NEWRANGE
         LDA   :OLDVAL
         SEC
         SBC   :OLDMIN
         STA   :VALRANGE
         LDA   #00
         TAY
         STY   :NUM1HI
         BEQ   :ENTLP
:DOADD
         CLC
         ADC   :VALRANGE
         TAX
         TYA
         ADC   :NUM1HI
         TAY
         TXA
:MLP
         ASL   :VALRANGE
         ROL   :NUM1HI
:ENTLP
         LSR   :NEWRANGE
         BCS   :DOADD
         BNE   :MLP
         STA   :MULRANGE
         STY   :MULRANGE+1
:DIVIDE
         LDA   #0
         STA   :REMAIN
         STA   :REMAIN+1
         LDX   #16
:DIVLP
         ASL   :MULRANGE
         ROL   :MULRANGE+1
         ROL   :REMAIN
         ROL   :REMAIN+1
         LDA   :REMAIN
         SEC
         SBC   :OLDRANGE
         TAY
         LDA   :REMAIN+1
         SBC   :OLDRANGE+1
         BCC   :SKIP
         STA   :REMAIN+1
         STY   :REMAIN
         INC   :DIVRANGE
:SKIP    DEX
         BNE   :DIVLP
         LDA   :DIVRANGE  
         CLC
         ADC   :NEWMIN
         STA   :NEWVAL
         LDA   RETADR+1
         PHA
         LDA   RETADR
         PHA
         LDA   :NEWVAL    
         STA   RETURN
         LDX   #1
         STX   RETLEN
         RTS
:OLDRANGE DS   2
:NEWRANGE DS   2
:MULRANGE DS   2
:DIVRANGE DS   2
:VALRANGE DS   2
:OLDVAL  DS    2
:NEWVAL  DS    2
:OLDMIN  DS    2
:NEWMIN  DS    2
:OLDMAX  DS    2
:NEWMAX  DS    2
:NUM1HI  DS    2
:REMAIN  DS    2
