Skip to navigation

Maths (Arithmetic): GetRandomNumber

Name: GetRandomNumber [Show more] Type: Subroutine Category: Maths (Arithmetic) Summary: Set A to a random number
Context: See this subroutine in context in the source code References: This subroutine is called as follows: * DitherScreenBuffer calls GetRandomNumber * DrawRandomDots calls GetRandomNumber * UpdateScannerNow calls GetRandomNumber
.GetRandomNumber \ We generate a new random number by taking our 24-bit \ generator in randomGenerator(2 1 0) and adding it to \ itself but shifted left by two places, so on each \ iteration we do the following: \ \ randomGenerator(2 1 0) += shiftGenerator(2 1 0) \ \ We then return the high byte of randomGenerator(2 1 0) \ as the next random number LDA randomGenerator \ Set shiftGenerator(2 1 0) = randomGenerator(2 1 0) STA shiftGenerator LDA randomGenerator+1 STA shiftGenerator+1 LDA randomGenerator+2 STA shiftGenerator+2 ASL shiftGenerator \ Shift shiftGenerator(2 1 0) left by two places ROL shiftGenerator+1 ROL shiftGenerator+2 ASL shiftGenerator ROL shiftGenerator+1 ROL shiftGenerator+2 LDA randomGenerator \ Set randomGenerator(2 1 0) += shiftGenerator(2 1 0) CLC \ ADC shiftGenerator \ This also sets A to byte #2 of randomGenerator, so we STA randomGenerator \ return this as the next random number LDA randomGenerator+1 ADC shiftGenerator+1 STA randomGenerator+1 LDA randomGenerator+2 ADC shiftGenerator+2 STA randomGenerator+2 RTS \ Return from the subroutine