Jump to content
IGNORED

Generating "Random" numbers.


Recommended Posts

I was wondering, what is a good, efficient way to generate "random" numbers in assembly?

 

From the old cc65 archives this is my favourite routine for generating starfields.

* RANDOM.M65
*
* Offered by Rob Nicholson.
* It works great _and_ is fast !
* ported :) by 42BS
*
* C: int random()
* Added routine to set the seed to be able to create pseudorandom
* sequences by Karri Kaksonen
* C: void srandom(int seed)

			xref tstax
			xref popax


			global _random
			global _srandom
_srandom::
			jsr popax
			sta random_seed
			stx random_seed+1
			eor #$55
			sta random_seed+2
			rts
_random::
			lda random_seed+1			  ; havn't a clue what this does (
me neither 42BS;-)
			tax
			eor #$80
			rol A
			rol random_seed+2
			ror random_seed+1
			lda random_seed
			sta random_seed+1

			ror A
			eor random_seed+2
			stx random_seed+2
			sta random_seed
;
;
; 16-Bit random in AX
;
			pha
			txa
			lsr A
			tax
			pla
			ror A
;
; shift right once => unsigned random-number
;
			jmp tstax	  ; set cc

random_seed	 dc.w 0
last_random	 dc.b 35

 

This is the best implementation I have seen for practical game programming.

 

--

Karri

Link to comment
Share on other sites

Do a search, there are several threads here on AA devoted to that very subject.

 

You want a linear feedback shift register (LFSR). Do a search.

 

I will say that for the 2600 anything more than an 8-bit random number is almost always overkill and a waste of your precious RAM.

Edited by vdub_bobby
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...