
 1000  *---------------------------------
 1010  *      STAND-ALONE RANDOM FUNCTION
 1020  *      ---------------------------
 1030  *
 1040  *      GET A RANDOM NUMBER BETWEEN 0 AND X-1
 1050  *
 1060  *      CALL:  VALUE X IN Y- AND A-REGISTERS
 1070  *             JSR RANDOM
 1080  *    RETURN:  RANDOM NUMBER IN Y- AND A-REGISTERS
 1090  *             LO-BYTE IN Y, HI-BYTE IN A
 1100  *---------------------------------
 1110  MON.RNDL   .EQ $4E
 1120  MON.RNDH   .EQ $4F
 1130  MON.PRBYTE .EQ $FDDA
 1140  MON.COUT   .EQ $FDED
 1150  *---------------------------------
 1160  RANDOM STY LIMIT    SAVE LIMIT VALUE
 1170         STA LIMIT+1
 1180         LDA MON.RNDH GET SEED HI-BYTE
 1190         BNE .1       BE SURE SEED BTWN 1 AND 7FFF
 1200         CMP MON.RNDL SET CARRY IF BOTH BYTES ZERO
 1210         ADC #0       CHANGE 0000 TO 0100
 1220  .1     AND #$7F     MAKE SURE NOT LARGER THAN 7FFF
 1230         STA MON.RNDH
 1240         STA VALUE+1
 1250         LDA MON.RNDL
 1260         STA VALUE
 1270         LDA #0
 1280         STA VALUE+2
 1290         STA VALUE+3
 1300  *---------------------------------
 1310         LDY #17      LOOP TO MAKE NEXT RANDOM VALUE
 1320  .2     LDA MON.RNDH (WOZNIAK'S ALGORITHM)
 1330         ASL
 1340         CLC
 1350         ADC #$40
 1360         ASL
 1370         ROL MON.RNDL
 1380         ROL MON.RNDH
 1390         DEY
 1400         BNE .2
 1410  *---------------------------------
 1420         LDA LIMIT
 1430         ORA LIMIT+1
 1440         BEQ .5       RETURN ZERO
 1450  *---------------------------------
 1460  *      DIVIDE RANDOM VALUE (1-7FFF) BY LIMIT
 1470  *      AND USE REMAINDER (0<=REMAINDER<LIMIT)
 1480  *---------------------------------
 1490         LDY #16      LOOP FOR 16-BITS
 1500  .3     ASL VALUE    DOUBLE DIVIDEND
 1510         ROL VALUE+1
 1520         ROL VALUE+2
 1530         ROL VALUE+3
 1540         LDA VALUE+2
 1550         CMP LIMIT
 1560         LDA VALUE+3
 1570         SBC LIMIT+1
 1580         BCC .4       PARTIAL DIVIDEND < LIMIT
 1590         STA VALUE+3
 1600         LDA VALUE+2  CARRY IS SET, SUBTRACT
 1610         SBC LIMIT    LO-BYTE OF LIMIT
 1620         STA VALUE+2
 1630         INC VALUE    SET BIT IN QUOTIENT
 1640  .4     DEY
 1650         BNE .3
 1660  *---------------------------------
 1670  *      RETURN RANDOM VALUE MOD LIMIT
 1680  *---------------------------------
 1690  .5     LDA VALUE+3  PICK UP REMAINDER FROM DIVISION
 1700         LDY VALUE+2
 1710         RTS
 1720  *---------------------------------
 1730  LIMIT  .BS 2
 1740  VALUE  .BS 4
 1750  *---------------------------------
 1760  TEST.RANDOM
 1770         LDA #160
 1780         STA COUNT
 1790  .1     LDY #1000
 1800         LDA /1000
 1810         JSR RANDOM   RND(1000)
 1820         JSR MON.PRBYTE
 1830         TYA
 1840         JSR MON.PRBYTE
 1850         LDA #$A0     PRINT BLANK
 1860         JSR MON.COUT
 1870         DEC COUNT
 1880         BNE .1
 1890         RTS
 1900  COUNT  .BS 1

