Prev: 60130 Up: Map Next: 60191
60133: Generate Pseudo-Random Number
Input
A Maximum value
Output
A Random number
PseudoRandom 60133 CP 2 Is A less than 2? If not, jump to PseudoRandom_Mix.
60135 JR NC,PseudoRandom_Mix
60137 LD A,2 Load A with 2.
Clamp the range to at least 2, mix the seed at RandomSeedScratch, and reduce into 1…max.
PseudoRandom_Mix 60139 LD (60132),A Store A as the random-range maximum at RandomRangeMaximum.
60142 PUSH HL Stash HL, DE and BC on the stack.
60143 PUSH DE
60144 PUSH BC
60145 LD DE,(60130) Load DE from RandomSeedScratch.
60149 LD H,E Copy E into H.
60150 LD L,253 Load L with 253.
60152 LD A,D Copy D into A for later use.
60153 OR A Is A zero?.
60154 SBC HL,DE Subtract DE from HL, including the previous success marker.
60156 SBC A,0 Subtract the previous success marker from A.
60158 SBC HL,DE Subtract DE from HL, including the previous success marker.
60160 SBC A,0 Subtract the previous success marker from A.
60162 LD E,A Copy A into E as a saved coordinate / value.
60163 LD D,0 Clear D (high byte of a pointer/offset).
60165 SBC HL,DE Subtract DE from HL, including the previous success marker.
60167 JR NC,PseudoRandomContinue Jump to PseudoRandomContinue if the result did not underflow.
60169 INC HL Advance the data pointer in HL by one byte.
Write the mixed seed back to RandomSeedScratch and accumulate it against the range.
PseudoRandomContinue 60170 LD (60130),HL Store HL in RandomSeedScratch.
60173 PUSH HL Copy HL into DE (using the stack).
60174 POP DE
60175 LD A,(60132) Load A with the random-range maximum at RandomRangeMaximum.
60178 DEC A Decrease A by one.
60179 LD B,A Keep a copy of A in B.
60180 XOR A Clear A to 0.
Fold the seed word into the result by repeated addition.
PseudoRandomContinueB 60181 ADD HL,DE HL+=DE.
60182 JR NC,PseudoRandomContinueB2 Jump to PseudoRandomContinueB2 if the result did not underflow.
60184 INC A Step the print-position / counter on by one.
Finish the accumulate loop and restore the caller's registers.
PseudoRandomContinueB2 60185 DJNZ PseudoRandomContinueB Decrease the loop counter in B and jump to PseudoRandomContinueB if more iterations remain.
60187 POP BC Restore BC, DE and HL from the stack.
60188 POP DE
60189 POP HL
60190 RET Return.
View the equivalent code in;
Prev: 60130 Up: Map Next: 60191