Prev: EAE2 Up: Map Next: EB1F
EAE5: Generate Pseudo-Random Number
Input
A Maximum value
Output
A Random number
PseudoRandom EAE5 CP $02 Is A less than 02? If not, jump to PseudoRandom_Mix.
EAE7 JR NC,PseudoRandom_Mix
EAE9 LD A,$02 Load A with 02.
Clamp the range to at least 02, mix the seed at RandomSeedScratch, and reduce into 01…max.
PseudoRandom_Mix EAEB LD ($EAE4),A Store A as the random-range maximum at RandomRangeMaximum.
EAEE PUSH HL Stash HL, DE and BC on the stack.
EAEF PUSH DE
EAF0 PUSH BC
EAF1 LD DE,($EAE2) Load DE from RandomSeedScratch.
EAF5 LD H,E Copy E into H.
EAF6 LD L,$FD Load L with FD.
EAF8 LD A,D Copy D into A for later use.
EAF9 OR A Is A zero?.
EAFA SBC HL,DE Subtract DE from HL, including the previous success marker.
EAFC SBC A,$00 Subtract the previous success marker from A.
EAFE SBC HL,DE Subtract DE from HL, including the previous success marker.
EB00 SBC A,$00 Subtract the previous success marker from A.
EB02 LD E,A Copy A into E as a saved coordinate / value.
EB03 LD D,$00 Clear D (high byte of a pointer/offset).
EB05 SBC HL,DE Subtract DE from HL, including the previous success marker.
EB07 JR NC,PseudoRandomContinue Jump to PseudoRandomContinue if the result did not underflow.
EB09 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 EB0A LD ($EAE2),HL Store HL in RandomSeedScratch.
EB0D PUSH HL Copy HL into DE (using the stack).
EB0E POP DE
EB0F LD A,($EAE4) Load A with the random-range maximum at RandomRangeMaximum.
EB12 DEC A Decrease A by one.
EB13 LD B,A Keep a copy of A in B.
EB14 XOR A Clear A to 00.
Fold the seed word into the result by repeated addition.
PseudoRandomContinueB EB15 ADD HL,DE HL+=DE.
EB16 JR NC,PseudoRandomContinueB2 Jump to PseudoRandomContinueB2 if the result did not underflow.
EB18 INC A Step the print-position / counter on by one.
Finish the accumulate loop and restore the caller's registers.
PseudoRandomContinueB2 EB19 DJNZ PseudoRandomContinueB Decrease the loop counter in B and jump to PseudoRandomContinueB if more iterations remain.
EB1B POP BC Restore BC, DE and HL from the stack.
EB1C POP DE
EB1D POP HL
EB1E RET Return.
View the equivalent code in;
Prev: EAE2 Up: Map Next: EB1F