It is currently Mon Oct 20, 2014 4:49 pm

All times are UTC [ DST ]




Post new topic Reply to topic  [ 4 posts ] 
Author Message
 Post subject: What's good for RANDOM?
PostPosted: Thu Oct 21, 2010 11:34 am 
Offline
 Profile

Joined: Fri Nov 07, 2008 2:28 pm
Posts: 65
Hi,

Anyone know a good hardware address or/and routine to use for random ?

I am trying LDA $FE44 but it doesn't seem very random. (saw that in some game)

What I ideally need is a random number between 0 and 'N' as well.

- PJ


Top
 
PostPosted: Thu Oct 21, 2010 2:28 pm 
Offline
User avatar
 Profile

Joined: Mon Jan 07, 2008 6:46 pm
Posts: 380
Location: Málaga, Spain
You can try combining a load from &FE44 with a circular table of random numbers, e.g.
Code:
.random
; increment and fetch random number table index
LDX randindex
INX
CPX #16
BCC wrapindex
LDX #0
.wrapindex
STX randindex
; fetch random number
LDA randtable,X
EOR &FE44
STA randtable,X
RTS

.randtable
; put 16 random numbers here


If you want to limit the number to between 0 and 2^n-1, that's easily done with an AND. If it's a non-power-of-two, e.g. between 0 and 23, unfortunately I can't think of an easier way than:
Code:
.try
JSR random
AND #31
CMP #24
BCS try


Top
 
PostPosted: Wed Dec 01, 2010 9:08 pm 
Offline
 Profile

Joined: Fri Nov 07, 2008 2:28 pm
Posts: 65
Thanks for that - the AND trick is pretty neat - I used it in my recent BeebSID game.


Top
 
PostPosted: Thu Dec 02, 2010 9:30 am 
Offline
 Profile

Joined: Tue Apr 08, 2008 11:50 pm
Posts: 12
I recently used the LSB of the countdown interval timer (wow, I've actually read a manual for this) to pull a random seed for one of my proggies.

lda &02A0

Lets not get too involved in how non-random this is going to be 'cause my program is started at exactly the same nano-second after the computer is turned on each time... ;)

and then for a fast '8 bit' random number;

lsr A
bcs skip1
eor #&B2
.skip1
; random number in accumulator

Courtesy of some Commodore programming website I ran into.


Top
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 4 posts ] 

All times are UTC [ DST ]


Who is online

Users browsing this forum: No registered users and 1 guest


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
cron