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 Coding > Random number generation
2005-06-20 18:49
Wanderer
Account closed

Registered: Apr 2003
Posts: 478
Random number generation

I've been out of coding for so long that I no longer remember how to generate random numbers.

I remember I used to use a method where I'd pull a note from the SID chip music being played and add on the 'seconds' from the built in clock. It actually worked pretty good for my intentions.

I know BASIC has a RND function, and pardon me for asking but what method would YOU use to obtain a random number. There must be an easier method than what I'm using. I'm looking for an ASSEMBLY method :)

It's been so long, that I really forget all I ever learned :)
2005-06-20 18:59
AüMTRöN

Registered: Sep 2003
Posts: 44
Here is some example, kinda works... maybe not the best way. Used as a subroutine here. Result in A.
random: lda #$00 ; generate random number from 0-255
        eor $dc04
        eor $dc05
        eor $dd04
        adc $dd05
        eor $dd06
        eor $dd07
        sta random+1
        rts

EDIT: Hey that was my first CSDb post. Love me!
2005-06-20 20:43
Tch
Account closed

Registered: Sep 2004
Posts: 512
Lol,I also used the timer for random numbers.
If you feel like using it again,it is located at $00A2.
That´s the `seconds´ ofcoz.. ;)
2005-06-20 20:50
cadaver

Registered: Feb 2002
Posts: 1160
You could also add/eor/shift on bytes from the code. Works best if your codearea is in one place separate from datas.
2005-06-20 22:09
Cybernator

Registered: Jun 2002
Posts: 154
About SID's wave-output method: just set the 3rd channel to generate noise signal, and mask it with the TEST bit. Then read the output from $d41b or $d41c (I think it was one of these locations). Damn, I must be gettin ol. :( Will turn 20 soonish. Feel free to wish me a happy birthday when time comes. :P

Ok, back on topic: this won't work reliably if Siddy rocks some cool mus coz the 3rd channel has to produce pure noise. But then, it really depends on how random you want your numbers to be. :)
2005-06-20 22:31
Tch
Account closed

Registered: Sep 2004
Posts: 512
"Damn, I must be gettin ol. :( Will turn 20 soonish."

Aaah,the pain...where is my pension check. ;)
Nice idea,but i recommend running it before the part starts.
Someone mentioned it would be ´daring´ to make something without music,but I disagree.
Music is essential and I am very happy to see so much activity on the SID side of the Commodore! 8)

Anyway,I am happy to see a youngster like yourself,taking interest in this old machine. ;)
2005-06-21 02:52
Wanderer
Account closed

Registered: Apr 2003
Posts: 478
Quote: "Damn, I must be gettin ol. :( Will turn 20 soonish."

Aaah,the pain...where is my pension check. ;)
Nice idea,but i recommend running it before the part starts.
Someone mentioned it would be ´daring´ to make something without music,but I disagree.
Music is essential and I am very happy to see so much activity on the SID side of the Commodore! 8)

Anyway,I am happy to see a youngster like yourself,taking interest in this old machine. ;)


Thanks for your help. I'm going to try the above suggestions for my upcoming demo :)
2005-06-21 09:58
Hein

Registered: Apr 2004
Posts: 954
Quote: About SID's wave-output method: just set the 3rd channel to generate noise signal, and mask it with the TEST bit. Then read the output from $d41b or $d41c (I think it was one of these locations). Damn, I must be gettin ol. :( Will turn 20 soonish. Feel free to wish me a happy birthday when time comes. :P

Ok, back on topic: this won't work reliably if Siddy rocks some cool mus coz the 3rd channel has to produce pure noise. But then, it really depends on how random you want your numbers to be. :)


You can turn off Channel 3 by setting bit 7 on d418 I think. (At least it worked in Goattracker)

But doesnt d41b always output a random number, the moment u turn on c64?
2005-06-21 10:15
Graham
Account closed

Registered: Dec 2002
Posts: 990
The SID noise is also just a simple EOR feedback, you can write that in software aswell. It's propably the best anyway, just use timer/raster/keyboard/SID values as seed and do the rest via a normal random number algorithm.

Example:

ASL rnd
ROL rnd+1
BCC .nofeedback
LDA rnd
EOR #$21
STA rnd
lda rnd+1
EOR #$10
STA rnd+1
.nofeedback:
2005-06-21 16:02
MagerValp

Registered: Dec 2001
Posts: 1078
The problem with that routine is that if you hit 0, it stays at 0. White Flame posted a neat 8-bit pseudo random routine that walks through all numbers in a sorta-random order, and never gets stuck: http://www.white-flame.com/prng.txt
2005-06-24 09:59
Case

Registered: Aug 2002
Posts: 142
If you download my "Q-Noter 3" you can find a pretty good random number generating routine in there.
2008-06-23 09:01
HCL

Registered: Feb 2003
Posts: 728
Waking up this thread again :). Is there any known way to modify the eor-feedback routine (see latest post by Graham) to generate *a bit more* predictable values?.. What i'm after is to have values that increase, but still are a bit random, keeping the property of not missing any values.

What i want to do is a fade-effect which fills the screen with dots/pixels from right to left in a random manner. Something like:

for i=0 to 319
xpos = i + 5*rnd(0)
next i

The above basic shit will of course miss some values and returning other values twice, and.. it's in basic :P. Any ideas?
2008-06-23 09:32
Mace

Registered: May 2002
Posts: 1799
HCL: a way to not miss values is to use the random generator to sort a table instead of generating values.
If a number is missed, it means that the value will be the same as the table index.
Catch my drift?

Then again... if it's a fader you only use once, you could also just pre-calc the random and fix errors by hand ;-)
2008-06-23 10:27
Monte Carlos

Registered: Jun 2004
Posts: 359
I have never done it, but i would try it the following way:
Start at an arbitrary x position, add a prime a little smaller than 320 and use the modulo it as the next x position. If i am right, all the x coordinates should be returned exactly one time until you reach a common multiple of your prime an 320.

Monte
2008-06-23 11:46
Partz
Account closed

Registered: Jun 2008
Posts: 17
Quote: Waking up this thread again :). Is there any known way to modify the eor-feedback routine (see latest post by Graham) to generate *a bit more* predictable values?.. What i'm after is to have values that increase, but still are a bit random, keeping the property of not missing any values.

What i want to do is a fade-effect which fills the screen with dots/pixels from right to left in a random manner. Something like:

for i=0 to 319
xpos = i + 5*rnd(0)
next i

The above basic shit will of course miss some values and returning other values twice, and.. it's in basic :P. Any ideas?


I did something similar to this on the PC in C. I plotted n number of pixels per frame some of which were duplicated and some pixels never hit at all. At a certain point in time enough of the screen is filled with pixels such that doing a screen wipe still gives the perception that the screen was filled by the random number generator. It didn't matter that my random number generator didnt cover all values.
2008-06-23 12:00
HCL

Registered: Feb 2003
Posts: 728
@Partz: OMG, that sounds like the way i *don't* want to do it :).

@Mace: Hmm, sorting a table!? I don't want to sort, i want numberzzz ;). ..and precalcing is not the answer either, but thnx anyways :).

@Monte Carlos: Yeah!! That's the kind of answer i want. But.. sounds too complicated to even try :/. Well, let's say we have 0-39 instead of 319.. I start with x = 7. Add prime smaller than 40 = 37, i get 7 + 37 = 44. then i do modulo 39 -> 4. Hmm, i get the sequence.. 7,4,1,38,35,33,30,27.. a bit too predictable ;). Need some magic eor shit also, don't i?
2008-06-23 13:26
Shadow
Account closed

Registered: Apr 2002
Posts: 355
That White Flame 0-ff randomizer is great, I am so stealing that for my BASIC-screen remove effect!
2008-06-23 13:45
Mace

Registered: May 2002
Posts: 1799
HCL wrote:
Quote:
Hmm, sorting a table!? I don't want to sort, i want numberzzz ;)
Sure, but you know what the range is, right? Then put that in a table and random re-arrange/sort those...
But I think you're smart enough to understand what I meant the first time, so... have it your way ;)
2008-06-23 14:08
HCL

Registered: Feb 2003
Posts: 728
@Mace: Oh, so you mean.. start with a linear table from 0 to (range - 1). Then randomly scramble *a little* using bouble sort.. Ah, there the sort comes in :). Is that it? Hehe sounds really cute :).
2008-06-23 14:37
Monte Carlos

Registered: Jun 2004
Posts: 359
Perhaps my fault was to choose a prime close to 40.
Starting at zero, 17 gives: 0,17,34,11,28,5,22,9,26,3,20,37,14,...
This is much more random. Perhaps the clue lies in the finding of a suitable prime.
I'll try this @ home. It's bugging me too, now!

Monte
2008-06-23 15:42
Mace

Registered: May 2002
Posts: 1799
HCL wrote:
Quote:
@Mace: Oh, so you mean.. start with a linear table from 0 to (range - 1). Then randomly scramble *a little* using bouble sort.. Ah, there the sort comes in :). Is that it? Hehe sounds really cute :).
Indeed.
See, I didn't underestimate you ;-)
2008-06-23 19:35
Monte Carlos

Registered: Jun 2004
Posts: 359
@HCL:

I tried my idea now, and it suxx. Don't do it this way.

Monte
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
psenough
sln.pixelrat
iceout/Avatar/HF
syntaxerror
The MeatBall
t0m3000/hf^boom!^ibx
Ramon B5/DESiRE
Codey/Second Dimension
E$G/HF ⭐ 7
Guests online: 136
Top Demos
1 Next Level  (9.7)
2 13:37  (9.7)
3 Mojo  (9.7)
4 Coma Light 13  (9.6)
5 Edge of Disgrace  (9.6)
6 What Is The Matrix 2  (9.6)
7 The Demo Coder  (9.6)
8 Uncensored  (9.6)
9 Comaland 100%  (9.6)
10 Wonderland XIV  (9.6)
Top onefile Demos
1 No Listen  (9.6)
2 Layers  (9.6)
3 Cubic Dream  (9.6)
4 Party Elk 2  (9.6)
5 Copper Booze  (9.6)
6 X-Mas Demo 2024  (9.5)
7 Dawnfall V1.1  (9.5)
8 Rainbow Connection  (9.5)
9 Onscreen 5k  (9.5)
10 Morph  (9.5)
Top Groups
1 Performers  (9.3)
2 Booze Design  (9.3)
3 Oxyron  (9.3)
4 Censor Design  (9.3)
5 Triad  (9.3)
Top Crackers
1 Mr. Z  (9.9)
2 Antitrack  (9.8)
3 OTD  (9.8)
4 Fungus  (9.8)
5 S!R  (9.8)

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