Log inRegister an accountBrowse CSDbHelp & documentationFacts & StatisticsThe forumsAvailable RSS-feeds on CSDbSupport CSDb Commodore 64 Scene Database
You are not logged in - nap
CSDb User Forums


Forums > C64 Productions > Random Numbers
2003-01-29 18:32
Dr. Jay
Account closed

Registered: Jan 2003
Posts: 32
Random Numbers

Just wanted to share. This has probably been discussed a million times, but in case it hasn't ... was doing a search for random numbers. Some of the old hardware tricks like $d800 don't work with specific versions of the machine and certainly not in all emulators. I found this snippet of code which, if I remember correctly, was from boulderdash:

random eor $dc04
eor $dc05
eor $dd04
adc $dd05
eor $dd06
eor $dd07
rts

Tried using it - I always test random numbers by using the sequence to plot pixels. If you have a bad generator, you can easily see patterns. I could tell this pattern above was clustering on byte-boundaries after watching a few minutes in the plotter. I felt like it was because it was synched too much to the timers and that something would have to throw it out. Then I started thinking about how stable rasters are a $#$@#$@$ and if the call to this routine doesn't always come on a stable raster then we'll have some cycles out of synch ... so I made this simple modification:

random lda #$00
eor $dc04
eor $dc05
eor $dd04
adc $dd05
eor $dd06
eor $dd07
sta random+1
rts

And it appears to work like a charm - much more random! Just uses a seed to feed into the timer sequence, rather than the timer sequence alone!

 
... 11 posts hidden. Click here to view all posts....
 
2003-01-30 00:34
CyberBrain
Administrator

Posts: 392
what a kewl and *elegant* way to make random numbers!
2017-03-04 12:18
xIII

Registered: Nov 2008
Posts: 210
Sorry for bringing this very old thread back but is this still a good way to make random numbers ?

How should I change this routine to get random numbers from 0 to 9 ?
2017-03-04 12:28
chatGPZ

Registered: Dec 2001
Posts: 11136
this has never been a good way in the first place :) its not even faster than a proper 16bit lfsr for that matter....

to get numbers in a specific range, the "correct" way (which preserves even distribution of all numbers) is to generate a number in range 0...1, and then multiply the result by your range.

if you can live with somewhat uneven distribution, first generate a number in 0...255 range, then use the result as index into a one page table that contains all numbers you want to get
2017-03-04 14:42
Oswald

Registered: Apr 2002
Posts: 5023
http://codebase64.org/doku.php?id=base:6502_6510_maths#random_n..
2017-03-04 14:46
xIII

Registered: Nov 2008
Posts: 210
Thx Groepaz.
I did some more research @codebase64 and found this random generator and in combination with the table you suggested it works fine for me.

.var sr = $ff
	lda sr+1
	asl
	asl
	eor sr+1
	asl
	eor sr+1
	asl
	asl
	eor sr+1
	asl
	rol sr
	rol sr+1

	tax
	lda rnd_table,x
	sta rnd_number


rnd_table: .for(var i=0;i<256;i++) .byte random()*9+48
2017-03-04 14:49
xIII

Registered: Nov 2008
Posts: 210
Thx Oswald :)
2017-03-04 15:34
chatGPZ

Registered: Dec 2001
Posts: 11136
Quote:
.byte random()*9+48

i'd use something like .byte (i%9)+48 instead ... if only because it will always assemble into the same table
2017-03-04 18:31
xIII

Registered: Nov 2008
Posts: 210
Quote: Quote:
.byte random()*9+48

i'd use something like .byte (i%9)+48 instead ... if only because it will always assemble into the same table


Kickassembler doesn't seem to like this ?
Maybe because I use C64 Kickass IDE v1.2 which is old?
2017-03-04 18:40
chatGPZ

Registered: Dec 2001
Posts: 11136
no idea what kickass likes really :=) it must have some kind of modulo operator, i guess =P
2017-03-04 19:18
xIII

Registered: Nov 2008
Posts: 210
Quote: no idea what kickass likes really :=) it must have some kind of modulo operator, i guess =P

mod(random()*9,9)+48 seems to work.

But it's no problem if the table assembles different everytime ;)
Previous - 1 | 2 | 3 - Next
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
Sledge/Fairlight
Fred/Channel 4
Acidchild/Padua
Flavioweb/🇮🇹HF..
skull
sachy
Guests online: 108
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 Memento Mori  (9.6)
10 Bromance  (9.5)
Top onefile Demos
1 It's More Fun to Com..  (9.7)
2 Party Elk 2  (9.7)
3 Cubic Dream  (9.6)
4 Copper Booze  (9.5)
5 TRSAC, Gabber & Pebe..  (9.5)
6 Rainbow Connection  (9.5)
7 Dawnfall V1.1  (9.5)
8 Quadrants  (9.5)
9 Daah, Those Acid Pil..  (9.5)
10 Birth of a Flower  (9.5)
Top Groups
1 Nostalgia  (9.3)
2 Oxyron  (9.3)
3 Booze Design  (9.3)
4 Censor Design  (9.3)
5 Crest  (9.3)
Top Webmasters
1 Slaygon  (9.7)
2 Perff  (9.6)
3 Morpheus  (9.5)
4 Sabbi  (9.5)
5 CreaMD  (9.1)

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