Star Tip 3

Random Number Generator
by Jon Ritman
from Your Sinclair #20 (Aug.1987)

If you liked Head Over Heels and Batman, then you'll know that Jon Ritman, along with his partner Bernie Drummond, knows a thing or two about programming brilliant games. One of the most important things in a good arcade adventure, or so it seems, is a random number generator.

"This is the 32-bit random number generator routine I used in Batman and Head Over Heels. The routine is quite fast and returns a reasonable random number. This version returns with HL holding a 16-bit random number. However, if you need all 32 bits, simply add the instruction LD DE,(SEED+2) just before the RET at the end. The 32-bit seed may be changed to any value of your choice."

So here it is, for all of you assembly junkies, the listing, which can be located anywhere suitable in memory. For those of you that haven't got an assembler, there's also a hex dump of the code for you to load in via a suitable hex loader. For convenience we've assembled it to 30000 (7530 hex), and in case you're wondering, its length is 45 bytes. [It's actually 46 bytes. JimG]

SEED:       DB "Jon!"

            

RANDOM:     LD HL,(SEED+2)

            LD D,L

            ADD HL,HL

            ADD HL,HL

            LD C,H

            LD HL,(SEED)

            LD B,H

            RL B

            LD E,H

            RL E

            RL D

            ADD HL,BC

            LD (SEED),HL

            LD HL,(SEED+2)

            ADC HL,DE

            RES 7,H

            LD (SEED+2),HL

            JP M,RANDOM3

            LD HL,SEED

RANDOM2:    INC (HL)

            INC HL

            JR Z,RANDOM2

RANDOM3:    LD HL,(SEED)

            RET



30000:2ADD005529294C2A=548

30008:DB0044CB105CCB13=820

30016:CB120922DB002ADD=746

30024:00ED5ACBBC22DD00=973

30032:FA5A7521DB003423=796

30040:28FC2ADB00C90000=754

-- Another Fine Product transcribed by: Jim Grimwood, Weardale, England --