It is currently Sat Jan 19, 2019 1:10 am

 All times are UTC - 7 hours

 Page 1 of 1 [ 11 posts ]
 Print view Previous topic | Next topic
Author Message
 Post subject: Best way to have a number between #\$00 and #\$XX?Posted: Mon Jul 03, 2017 4:14 pm

Joined: Sun Jun 11, 2017 5:39 pm
Posts: 60
What is the best routine in assembly to have a number from #\$00 to a given number?

I have solution but they are complex, I think there is surely an easy with with the instructions?

Top

 Post subject: Re: Best way to have a number between #\$00 and #\$XX?Posted: Mon Jul 03, 2017 4:27 pm

Joined: Thu Jul 23, 2015 7:54 pm
Posts: 183
Location: USA
Do you mean like generating a random number between 0 and n?

Top

 Post subject: Re: Best way to have a number between #\$00 and #\$XX?Posted: Mon Jul 03, 2017 4:47 pm

Joined: Sun Jun 11, 2017 5:39 pm
Posts: 60
yes exactly what I mean!!

doing

LDA \$EF (random generator)
AND #\$0F

Some number won't be obtained...

Top

 Post subject: Re: Best way to have a number between #\$00 and #\$XX?Posted: Mon Jul 03, 2017 4:58 pm

Joined: Sun Sep 19, 2004 11:12 pm
Posts: 21003
Location: NE Indiana, USA (NTSC)
How are you currently populating the value at address \$00EF? What range did you want, and what do you plan to use the numbers for? Does the range vary from call to call? Sometimes this can inform which technique is best.

The general-purpose RNG I recommend nowadays is the LCG from cc65's C library. Sometimes, depending on the exact range I need and how quickly a value should repeat before being reused, I'll post-process the random stream with a least-recently-used generator, calculated over a circular buffer in a manner similar to RC4's mixer.

Top

 Post subject: Re: Best way to have a number between #\$00 and #\$XX?Posted: Mon Jul 03, 2017 5:02 pm

Joined: Thu Jul 23, 2015 7:54 pm
Posts: 183
Location: USA
Working with any number that isn't a power of 2 is always tricky.

The way I've been doing it is having a table if values in ROM that's a power of two (The most i've needed is 8 so far, so that's what I've been using), and using a random number as the index for which to choose. Simple and quick.

Code:
lda random
And #%00000111
Tax
Rts

Otherwise you have to use division, which is costly. IDK, I'm sure someone here has a better solution.

Top

 Post subject: Re: Best way to have a number between #\$00 and #\$XX?Posted: Mon Jul 03, 2017 5:15 pm

Joined: Sun Jun 11, 2017 5:39 pm
Posts: 60
Sogona: thanks I will use this way!! what is clever and what I was looking for!!

Top

 Post subject: Re: Best way to have a number between #\$00 and #\$XX?Posted: Mon Jul 03, 2017 5:26 pm

Joined: Sun Jun 11, 2017 5:39 pm
Posts: 60
tepples wrote:
How are you currently populating the value at address \$00EF? What range did you want, and what do you plan to use the numbers for? Does the range vary from call to call? Sometimes this can inform which technique is best.

The general-purpose RNG I recommend nowadays is the LCG from cc65's C library. Sometimes, depending on the exact range I need and how quickly a value should repeat before being reused, I'll post-process the random stream with a least-recently-used generator, calculated over a circular buffer in a manner similar to RC4's mixer.

thanks! will look at it!!

Top

 Post subject: Re: Best way to have a number between #\$00 and #\$XX?Posted: Mon Jul 03, 2017 5:56 pm

Joined: Fri Feb 27, 2009 2:35 pm
Posts: 315
Location: Fort Wayne, Indiana
I usually just keep trying until I get a number out of the randomizer that works. Like this is the code I would use if I wanted 0, 1, 2, 3 or 4 with equal chances:
Code:
loop:
jsr random  ; get a new random number, put it in the accumulator
and #7      ; cut down the range
cmp #5      ; throw out 5, 6 or 7
bcs loop

Top

 Post subject: Re: Best way to have a number between #\$00 and #\$XX?Posted: Mon Jul 03, 2017 7:02 pm

Joined: Sun Sep 19, 2004 11:12 pm
Posts: 21003
Location: NE Indiana, USA (NTSC)
Another way to get nearly uniformly distributed numbers from 0 to 4 without the time-uncertainty of looping calls to your RNG is to multiply the RNG's output by 5.
Code:
jsr random

; Multiply A by 5/4 and keep bits 8-6
sta tmp0
lsr a
lsr a
clc
adc tmp0  ; C:A = 0 to 319
and #\$C0  ; C:A = 0, 64, 128, 192, or 256
rol a
rol a
rol a

For 5 choices in particular, you can treat values 0-203 one way and 204-255 another, such as returning 4 for all values 204+ and using bits 1-0 of values 0-203.
Code:
jsr random
cmp #204
bcc not4
lda #4
bcs haveA
not4:
and #\$03
haveA:

Top

 Post subject: Re: Best way to have a number between #\$00 and #\$XX?Posted: Mon Jul 03, 2017 10:55 pm

Joined: Fri Nov 12, 2004 2:49 pm
Posts: 7649
Location: Chexbres, VD, Switzerland
NovaSquirrel wrote:
I usually just keep trying until I get a number out of the randomizer that works. Like this is the code I would use if I wanted 0, 1, 2, 3 or 4 with equal chances:
Code:
loop:
jsr random  ; get a new random number, put it in the accumulator
and #7      ; cut down the range
cmp #5      ; throw out 5, 6 or 7
bcs loop

Just want to point out that's also exactly what I'd do.

Top

 Post subject: Re: Best way to have a number between #\$00 and #\$XX?Posted: Tue Jul 04, 2017 4:18 pm

Joined: Sun Jun 11, 2017 5:39 pm
Posts: 60
NovaSquirrel wrote:
I usually just keep trying until I get a number out of the randomizer that works. Like this is the code I would use if I wanted 0, 1, 2, 3 or 4 with equal chances:
Code:
loop:
jsr random  ; get a new random number, put it in the accumulator
and #7      ; cut down the range
cmp #5      ; throw out 5, 6 or 7
bcs loop

Just want to point out that's also exactly what I'd do.

thx!! it's actually the easiest solution!!

Top

 Display posts from previous: All posts1 day7 days2 weeks1 month3 months6 months1 year Sort by AuthorPost timeSubject AscendingDescending
 Page 1 of 1 [ 11 posts ]

 All times are UTC - 7 hours

#### Who is online

Users browsing this forum: No registered users and 2 guests

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

Search for:
 Jump to:  Select a forum ------------------ NES / Famicom    NESdev    NESemdev    NES Graphics    NES Music    Homebrew Projects       2018 NESdev Competition       2017 NESdev Competition       2016 NESdev Competition       2014 NESdev Competition       2011 NESdev Competition    Newbie Help Center    NES Hardware and Flash Equipment       Reproduction    NESdev International       FCdev       NESdev China       NESdev Middle East Other    General Stuff    Membler Industries    Other Retro Dev       SNESdev       GBDev    Test Forum Site Issues    phpBB Issues    Web Issues    nesdevWiki