Wickeycolumbus Posted April 19, 2008 Share Posted April 19, 2008 (edited) I was wondering, what is a good, efficient way to generate "random" numbers in assembly? Edited April 19, 2008 by Wickeycolumbus Quote Link to comment Share on other sites More sharing options...
+karri Posted April 19, 2008 Share Posted April 19, 2008 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 Quote Link to comment Share on other sites More sharing options...
vdub_bobby Posted April 21, 2008 Share Posted April 21, 2008 (edited) 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 April 21, 2008 by vdub_bobby Quote Link to comment Share on other sites More sharing options...
Recommended Posts
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.