Random number generator

Here you can ask questions or provide insights about how to use efficiently 6502 assembly code on the Oric.
User avatar
Chema
Game master
Posts: 3014
Joined: Tue Jan 17, 2006 10:55 am
Location: Gijón, SPAIN
Contact:

Random number generator

Post by Chema »

Greetings,

As we are going to get rid of ROM in Space 1999, I need to replace the random number generator routine with one of my own. I don't need anything sophisticated, so it would be best if it is quick and compact.

Any ideas out there?

Of course one can allways copy the ROM routine (I don't have it, btw), but I wondered if there was any simpler and more elegant solution for this case.

Cheers.
User avatar
carlsson
Pilot Officer
Posts: 127
Joined: Thu Jan 12, 2006 11:26 pm
Location: Västerås, Sweden

Post by carlsson »

Perhaps either of these two can be used?

http://www.ffd2.com/fridge/math/rand1.s
http://www.ffd2.com/fridge/math/rand2.s

You might also be interested in the books "Numerical Recipes", available online (through a free subscription system called FileOpen) in editions for C, Fortran 77 and Fortran 90:

http://www.nrbook.com/a/

Through those books, you could navigate yourself through handy algorithms for random number generators (and a lot more) and find one to implement if you don't have pseudo code from someplace else.
Anders Carlsson
User avatar
Dbug
Site Admin
Posts: 4444
Joined: Fri Jan 06, 2006 10:00 pm
Location: Oslo, Norway
Contact:

Post by Dbug »

Well, you can find one in the 4KKong archive here:
http://www.defence-force.org/download/4kkong.zip

First some variables:

Code: Select all

rand_low		.dsb 1		;// Random number generator, low part
rand_high		.dsb 1		;// Random number generator, high part
Then we need to initialise the values before first use

Code: Select all

	lda #23
	sta rand_low
	lda #35
	sta rand_high
Then the routine itself:

Code: Select all

_GetRand
	lda rand_high
	sta b_tmp1
	lda rand_low
	asl 
	rol b_tmp1
	asl 
	rol b_tmp1
	asl
	rol b_tmp1
	asl
	rol b_tmp1
	clc
	adc rand_low
	pha
	lda b_tmp1
	adc rand_high
	sta rand_high
	pla
	adc #$11
	sta rand_low
	lda rand_high
	adc #$36
	sta rand_high
	rts
For what I remember, the output of the two values is very different, one is "more random" than the other, and some bits are very not random.

Let's say it was good enough for my 4K Kong game :)
User avatar
Chema
Game master
Posts: 3014
Joined: Tue Jan 17, 2006 10:55 am
Location: Gijón, SPAIN
Contact:

Post by Chema »

Thanks! Will test one of those...

Cheers
Post Reply