[cc65] proposed replacement random generator

Date view Thread view Subject view

From: Sidney Cadot (sidney_at_janis.pds.twi.tudelft.nl)
Date: 2000-03-17 00:31:03


Hi all,


 I propose to replace the lib/rand.s code with the following:

===================== SNIPPET: lib/rand.s

; int rand (void);
; void srand (unsigned seed);

;  Uses 4-byte state.
;  Multiplier must be 1 (mod 4)
;  Added value must be 1 (mod 2)
;  This guarantees max. period (2**32)
;  Bits 8-22 are returned (positive 2-byte int)
;  where 0 is LSB, 31 is MSB.
;  This is better as lower bits exhibit easily
;  detectable patterns.

      	.export		_rand, _srand

.bss

rand:	.word	0		; LSW of seed
	.word   0		; MSW of seed

.code
	
_rand:
	clc
	lda	rand+0		; SEED *= $01010101
	adc     rand+1
	sta     rand+1
	adc     rand+2
	sta     rand+2
	adc     rand+3
	sta     rand+3
	clc
	lda     rand+0		; SEED += $31415927
	adc     #$27
	sta     rand+0
	lda     rand+1
	adc     #$59
	sta     rand+1
	pha
	lda     rand+2
	adc     #$41
	sta     rand+2
	and     #$7f
	tax
 	lda     rand+3
	adc     #$31
	sta     rand+3
	pla			; return bit 8-22 in (X,A)
	rts

_srand:	sta	rand+0		; Store the seed
	stx	rand+1
	lda     #0
	sta     rand+2          ; Set MSW to zero
	sta     rand+3
	rts

===================== END OF SNIPPET

This is a period 2^32 random generator (for any seed value), with quite
nice pseudo-random properties even in the low bits. It is slightly bigger
and slower than the generator in use now, but its behaviour is a lot
better.

Code is donated to the project; Ullrich may add a copyright notice as he
sees fit.

Best regards, Sidney

+--------------------------------------------------------------------------+
| -- Sidney Cadot - sidney_at_ch.twi.tudelft.nl - ITS/TU Delft - tst. 3850 -- |
+--------------------------------------------------------------------------+
|/* C-code to calculate day-of-week (1901-2099). Type name for man page. */|
|main(int c,char**v){exit(printf("%sday.\n","Use MM/DD/YYYY argument to c" |
|"alculate week\0Mon\0/0/Tues\0/0Wednes\0Thurs\0/Fri\0/0/Satur\0/Sun"+(*++ |
|v?42+((5*atoi(6+*v)+"@GSTDLXDP_at_HT@"[atoi(*v)])/4+atoi(3+*v))%7*7:0))/30);}|
+--------------------------------------------------------------------------+


----------------------------------------------------------------------
To unsubscribe from the list send mail to majordomo_at_musoftware.de with
the string "unsubscribe cc65" in the body(!) of the mail.


Date view Thread view Subject view

This archive was generated by hypermail 2.1.3 : 2001-12-14 22:05:35 CET