;
; SYSLIB Module Name:  SRAND
; Author:  Richard Conn
; SYSLIB Version Number:  2.0
; Module Version Number:  1.0
; Module Entry Points:
;	RND		RNDINIT		RNDSEED
; Module External References:
;	PRINT		CONDIN
;
;*
;*  EXTERNALS
;*
	EXT	PRINT
	EXT	CONDIN

;*
;*  RNDINIT -- INITIALIZE RANDOM NUMBER GENERATOR
;*	THIS ROUTINE PROMPTS THE USER FOR A KEYPRESS, AND THEN SETS THE
;*	SEE VALUE FOR THE RANDOM NUMBER GENERATOR (8-BIT)
;*  NO INPUT OR OUTPUT PARAMETERS
;*  GENERALLY, IT IS LEFT TO THE USER TO PRINT A MESSAGE BEFORE THIS ROUTINE
;*	IS CALLED
;*
RNDINIT::
	PUSH	PSW	; SAVE REGS
	PUSH	B
	MVI	B,0	; START AT 0
RNDIL:
	INR	B	; INCREMENT SEED
	CALL	CONDIN	; INPUT?
	JZ	RNDIL
	MOV	A,B	; GET SEED
RNDID:
	STA	SEED	; SAVE IT FOR LATER
	CALL	RND	; GENERATE A RANDOM NUMBER
	STA	ADDEND	; SAVE A INITIAL ADDEND
	POP	B	; RESTORE REGS
	POP	PSW
	RET

;*
;*  RNDSEED -- ALLOW USER TO SPECIFY A SEED VALUE FOR THE RANDOM NUMBER GEN
;*	SEED IS PASSED IN A; NO REGS AFFECTED
;*
RNDSEED::
	PUSH	PSW	; SAVE REGS
	PUSH	B
	JMP	RNDID	; SAVE SEED AND GENERATE ADDEND

;
;  RANDOM NUMBER SEED VALUE
;
SEED:
	DS	1	; 1 BYTE
ADDEND:
	DS	1	; ADDEND

;*
;*  RANDOM NUMBER GENERATOR
;*	RANDOM NUMBER RETURNED IN A
;*
RND::
	PUSH	H	; DON'T AFFECT HL
	LXI	H,SEED	; PT TO SEED
	MOV	A,M	; GET IT
	RLC		; PLAY WITH IT
	XRA	M
	RRC
	INX	H	; PT TO ADDEND
	INR	M	; INCREMENT IT
	ADD	M	; ADD IN ADDEND
	JPE	SKIP
	INR	M	; INCREMENT ADDEND AGAIN
SKIP:
	DCX	H	; PT TO SEED AGAIN
	MOV	M,A	; SAVE NEW SEED
	POP	H	; RESTORE HL
	RET

	END
