A82: Random Numbers


[Prev][Next][Index][Thread]

A82: Random Numbers




Here is the routine I used in SameGame 1.3 and LitesOut 1.0...modified to
accept a min and max number to generate.

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;                            ;;;
;;;  Random number generator   ;;;
;;;  INPUT-                    ;;;
;;;   b-minimum number         ;;;
;;;   c-maximum number         ;;;
;;;  OUTPUT-                   ;;;
;;;   a-random number between  ;;;
;;;     b and c (inclusive)    ;;;
;;;                            ;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

RANDOM:
	call GETRAND			;getrand is the routine in getrand.asm
RANDOM_LOOP:
	sub c				;subtract the max from a
	cp b				;compare it with the min
	jr nc, RANDOM_LOOP			;if greater than the min, keep looping
	add a, c				;add c for the last subtract, so it's b-c
	ret

#include GETRAND.asm

RANDSEED:					;permanent randseed inside program
	.dw $0000

I haven't tested this, but it should work...
getrand.asm is included with Lites Out 1.0 and SameGame 1.3, and it is
included below my signiture.
Have fun!

~Adamman

getrand.asm:

GETRAND:                        ; SET UP (RANDSEED) WITH A WORD
	PUSH HL                 ; MAYBE HAVE A COUNTER AT THE TITLE SCREEN
	PUSH BC                 ; IF YOU'RE WAITING FOR A KEY TO BE PRESSED.
	LD HL,(RANDSEED)        ;
	LD A,H                  ; JASON TODD (ALPHASOFT)
	ADD A,A
	RL L
	RL H
	ADD A,A
	RL L
	RL H
	ADD A,A
	RL L
	RL H
	LD BC,$7415
	ADD HL,BC
	LD (RANDSEED),HL
	LD A,H
	ADD A,L
	POP BC
	POP HL
	RET