[Coco] Example or a replacement for RND

Robert Gault robert.gault at att.net
Fri May 19 23:16:16 EDT 2017


It is easy enough to look for "random" number generators in books or on the Internet. Most will 
probably be much better than the one in the Basic ROM. So, rather than complaining about what the 
Coco has, why not try a replacement.
Below is a random number routine I used for an ml program based on something I read about random 
number generators. It is used in an ml graphics program.

RND	EQU	$116	LOCATION OF BASIC RANDOM NUMBER You can seed it with A=RND(-TIMER)

* THIS IS A FAST, HIGH GRADE RANDOM NUMBER GENERATOR
* LENGTH OF NON-REPEATING SEQUENCE = 2,147,483,647
* ENTER: REG.B = N+1 where you want 0<= RND <N
* EXIT:  REG.A = 0 TO 255 or 0 to .99609375
*        after MUL
*        REG.A = 0 to N
*	 REG.B = fraction
*	 REG.D = 256x(N+.99609375)

RAND	BSR	RND2
	BSR	RND2
	BSR	RND2
	BSR	RND2
	BSR	RND2
	LDA	RND
	MUL
	RTS
RND2	LDA	RND+3	GET 28TH BIT
	ANDA	#%00010000	KEEP 28TH BIT
	LSRA			00001000 29
	LSRA			00000100 30
	LSRA			00000010 31
	EORA	RND+3		KEEP 31ST BIT
	LSRA			shift
	LSRA			into carry
	ROR	RND
	ROR	RND+1
	ROR	RND+2
	ROR	RND+3
	RTS

Robert


More information about the Coco mailing list