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 > Another Random Generator
2017-05-05 19:17
xIII

Registered: Nov 2008
Posts: 210
Another Random Generator

For my little game Modulot I used this random generator I found on Codebase64:


GetRandom:
ldx #0
lda random+1
sta temp2
lda random
asl
rol temp2
asl
rol temp2
clc
adc random
pha
lda temp2
adc random+1
sta random+1
pla
adc #$11
sta random
lda random+1
adc #$36
sta random+1
sta RANDOMNUMBERS_TABLE,x
inx
bne GetRandom+2
rts

Unfortunately I found out (too late) that this routine always generates the same table. What I wanted was a different table every time this routine is called.

How can I create a random table that is different every time ?
2017-05-05 19:35
Smasher

Registered: Feb 2003
Posts: 520
my stupid solution: since in Modulot you have a title screen and game starts when you press space/fire just place a "inc my_counter" somewhere and recall level with "lda my_counter". use 2 bytes if you have many levels :)
2017-05-05 19:52
xIII

Registered: Nov 2008
Posts: 210
Yeah, that's stupid ... :p

It has to be a 256 bytes table.
I need the table for random pattern (level), random numbers, random mod,...

I found this:

FillRandom:		
        ldx #0
!l:	lda #$ff   // maximum frequency value
	sta $d40e  // voice 3 frequency low byte
	sta $d40f  // voice 3 frequency high byte
        lda #$80   // noise waveform, gate bit off
	sta $d412  // voice 3 control register
	lda $d41b
	sta Table_Random,x
	inx
	bne !l-
	rts


... but since these are SID registers, don't they mess up the music ? (maybe I should just try it).
2017-05-05 19:59
chatGPZ

Registered: Dec 2001
Posts: 11386
the one and only right way to do this is:

a) use some user interaction as random seed. ie start a timer and then read the timer value when user presses a key (to start the game or something)
b) from that point on use a PRNG as in the first post
2017-05-05 20:17
xIII

Registered: Nov 2008
Posts: 210
thx Groepaz,

a) In my IRQ: inc VALUE

b) the first lda random+1 is replaced by lda VALUE

this seems to work as I want.
2017-05-05 20:20
chatGPZ

Registered: Dec 2001
Posts: 11386
nooo thats not a good idea :). instead initialize random (and random+1) once with such counter value (cia1 timer will do fine)
2017-05-05 20:34
Smasher

Registered: Feb 2003
Posts: 520
I wonder where's the difference between Groepaz's and my solution :) just keep "inc value" in the irq while you play and the level is as random as RND
2017-05-05 20:41
chatGPZ

Registered: Dec 2001
Posts: 11386
look at the routine above. if you replace the first LDA random+1 by some LDA counter thing, the entire routine doesnt do anymore what its supposed to do - there wont be any feedback for the highbyte at all, breaking all the sequence properties.

rule of thumb: dont try to make your own PRNG - use an existing one that is proven to work. your own will most likely be (much) worse.
2017-05-05 21:15
Smasher

Registered: Feb 2003
Posts: 520
sure. but you just need 1 byte to have 256 random levels, 2 bytes for 64K levels, etc, no need to build a table and waste 256 bytes. also a table containing sorted numbers or one filled with random numbers doesn't change anything when the byte is picked up randomly from it. a table could make sense if you want to avoid repeated levels in a gameplay, but a game without repeated levels is boring, because when you are playing the 255th level you perfectly know which level you'll play next :)
2017-05-05 21:17
xIII

Registered: Nov 2008
Posts: 210
lda $dc04
sta random
lda $dc05
sta random+1


before the routine above?
2017-05-05 21:44
chatGPZ

Registered: Dec 2001
Posts: 11386
xIII: yes, exactly

and yes, building a table is pointless - with a given seed the generator produces the same sequence anyway.
 
... 3 posts hidden. Click here to view all posts....
 
Previous - 1 | 2 - 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
E$G/HF ⭐ 7
The Human Co../Maste..
Flashback
Shake/Role
Hoild/Ultimate Newco..
Chesser/Blazon
El Gato
Steffan/BOOM!
v3nt0r/ibex-crew
aNdy/AL/Cosine
t0m3000/hf^boom!^ibx
MCM/ONSLAUGHT
Fred/Channel 4
Hobbit/Laser Inc.
Guests online: 134
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 Layers  (9.6)
2 No Listen  (9.6)
3 Cubic Dream  (9.6)
4 Party Elk 2  (9.6)
5 Copper Booze  (9.6)
6 Dawnfall V1.1  (9.5)
7 Rainbow Connection  (9.5)
8 Onscreen 5k  (9.5)
9 Morph  (9.5)
10 Libertongo  (9.5)
Top Groups
1 Performers  (9.3)
2 Booze Design  (9.3)
3 Oxyron  (9.3)
4 Triad  (9.3)
5 Censor Design  (9.3)
Top Original Suppliers
1 Derbyshire Ram  (9.7)
2 Fungus  (9.3)
3 Black Beard  (9.2)
4 Baracuda  (9.2)
5 hedning  (9.1)

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