Log inRegister an accountBrowse CSDbHelp & documentationFacts & StatisticsThe forumsAvailable RSS-feeds on CSDbSupport CSDb Commodore 64 Scene Database
 Welcome to our latest new user maak ! (Registered 2024-04-18) You are not logged in - nap
CSDb User Forums


Forums > C64 Coding > Show me your (pseudo) random number generators!
2021-11-06 16:27
Frostbyte

Registered: Aug 2003
Posts: 165
Show me your (pseudo) random number generators!

So, let's assume you need a "good enough" random number generator, but can't use SID OSC3 for it. Either you need the third channel for actual sound production, you're sizecoding and can't afford the overhead of setting up SID, or whatever reason.

For size reasons, I needed such generator for Bubblesid. After many iterations, I came up with this:

lax $dc04     // load CIA1 timer lo to A and X
eor ($02, x)  // EOR w/ {addr}. Randomish vector from previous list X position


Thus 5 bytes, 10 cycles. $02 is start of the table on zero page where I stored 200 random values (and later sorted them), so on 2nd and subsequent runs the previously sorted values work as a "seed" for the routine. It works pretty well even if you don't use ZP for storing the values.

In the end I needed to save some more bytes and used X as the loop counter, thus I didn't use lax for the final release. Anyway, the above seemed to produce the highest quality "randomness" given its footprint and speed.

Which approach would you take, or have taken in the past? Just curious to see other ways to implement this, as I'm sure there are many alternatives, which produce better (more random) results, are faster and/or smaller in footprint, etc.

PS. To a pretty newbie asm coder, it's mind blowing how much one can do with very little code. I really want to keep learning this stuff!
2021-11-06 16:44
Krill

Registered: Apr 2002
Posts: 2822
Quoting Frostbyte
Which approach would you take, or have taken in the past?
You usually want a somewhat random seed (e.g., determined by current raster line and some other entropy sources), then a deterministic run of pseudo-random numbers (repeating at some point). Fixing the seed during development can then greatly ease fixing bugs etc.

As you mentioned size-coding, i took a very simple approach for Artefacts in the end (had an LFSR for a while during development):
				; get pseudo-random number
				; won't trash x, y
				; out: a - number in the range [$00..$ff]
.ifdef NTSC_VERSION
getrandom:      eor Artefacts+$0655
.else
getrandom:      eor Artefacts+$0653
.endif
                inc getrandom+$01
                rts
So a range of 256 bytes of its own code is read and EORed with whatever the caller passed in the accu.

Note that there weren't so many new numbers to get for a given amount of time, and that (iirc) the number of different callers to the routine was co-prime to 256.
It takes a very long time for the demo to loop and arrive at the same textures and heightmaps it starts with.

Oh, and about eor ($02,x) in your approach, do you initialise $02/3? Because if not, you really do not want to accidentally read things like certain IO registers and trigger creative bugs. =)
2021-11-06 17:12
Frostbyte

Registered: Aug 2003
Posts: 165
Ha, very cool approach! :)

Quote:
Oh, and about eor ($02,x) in your approach, do you initialise $02/3? Because if not, you really do not want to accidentally read things like certain IO registers and trigger creative bugs. =)

No init done, I didn't think (know) that simply reading something from memory (or IO) would alter the state of anything else, I always thought that a read is a read and that's it. What examples of this would there be?
2021-11-06 17:22
Krill

Registered: Apr 2002
Posts: 2822
Quoting Frostbyte
No init done, I didn't think (know) that simply reading something from memory (or IO) would alter the state of anything else, I always thought that a read is a read and that's it. What examples of this would there be?
Things like interrupt control registers which are read to acknowledge interrupts (http://unusedino.de/ec64/technical/aay/c64/cia113.htm) or collision detection registers (http://unusedino.de/ec64/technical/aay/c64/vic30.htm).

So yeah, reading certain IO registers may change internal state in the corresponding chips. :)
2021-11-06 17:38
Frostbyte

Registered: Aug 2003
Posts: 165
Ahhh right... Well, good to know. I guess I was lucky that my humble little prod was so simple, with no IRQs or care about sprite collisions etc. Otherwise I would have pulled my hair out trying to resolve seemingly randomly (pun intended) occurring bugs. :D
RefreshSubscribe to this thread:

You need to be logged in to post in the forum.

Search the forum:
Search   for   in  
All times are CET.
Search CSDb
Advanced
Users Online
tlr
LaLa/Remix64.com
Dymo/G★P
A3/AFL
Airwolf/F4CG
QuasaR/CENTRiC
Mibri/ATL^MSL^PRX
kbs/Pht/Lxt
master_hacker
The Syndrom/TIA/Pret..
Guests online: 115
Top Demos
1 Next Level  (9.8)
2 Mojo  (9.7)
3 Coma Light 13  (9.7)
4 Edge of Disgrace  (9.6)
5 Comaland 100%  (9.6)
6 No Bounds  (9.6)
7 Uncensored  (9.6)
8 Wonderland XIV  (9.6)
9 The Ghost  (9.6)
10 Bromance  (9.6)
Top onefile Demos
1 It's More Fun to Com..  (9.9)
2 Party Elk 2  (9.7)
3 Cubic Dream  (9.6)
4 Copper Booze  (9.5)
5 Rainbow Connection  (9.5)
6 TRSAC, Gabber & Pebe..  (9.5)
7 Onscreen 5k  (9.5)
8 Dawnfall V1.1  (9.5)
9 Quadrants  (9.5)
10 Daah, Those Acid Pil..  (9.5)
Top Groups
1 Oxyron  (9.3)
2 Nostalgia  (9.3)
3 Booze Design  (9.3)
4 Censor Design  (9.3)
5 Crest  (9.3)
Top Fullscreen Graphicians
1 Carrion  (9.8)
2 Joe  (9.8)
3 Duce  (9.8)
4 Mirage  (9.7)
5 Facet  (9.7)

Home - Disclaimer
Copyright © No Name 2001-2024
Page generated in: 0.116 sec.