Jump to content
IGNORED

Random Numbers + Data Statements


diogoandrei

Recommended Posts

Hello there, what's up?

 

As I was studying some small algorithms to update Havoc Bug, I ran into a small issue and I could not find a solution for it. Once again, here I am asking some advice :)

 

I am doing a small code to randomly pick data from a data statement, in a way that the last picked data would stay out of the next "lottery". The random data here feeds the background color just for the sake of testing. It's the random_ok.bas file and it's working fine. It looks like this:

 

	dim colorPointer = a
dim timer = b
colorPointer = (rand&7)

MainLoop

COLUBK = colorData[colorPointer]
drawscreen
timer = timer + 1 : if timer < 50 then goto MainLoop else timer = 0

CheckRepeatedColor

temp1 = (rand&7)
if temp1 = colorPointer then goto CheckRepeatedColor else colorPointer = temp1
goto MainLoop

data colorData
$30,$60,$D4,$1C,$92,$9A,$0E,$F6
end

 

When I add a new data to the statement and change...

 

(rand&7)

 

... into this:

 

(rand&

 

...since there's a new element in there, the code runs into a loop of two colors. That's in random_bug1.bas

 

I thought that maybe temp1 could be messing something, so I exchanged that for a variable. Looking like this:

 

CheckRepeatedColor

randomNumber = (rand&
if randomNumber = colorPointer then goto CheckRepeatedColor else colorPointer = temp1
goto MainLoop

 

With that, I ran into another strange situation. Now, the color changes once and then just stops, as if rand was just returning the same number always. That's in random_bug2.bas

 

I already fried my brain over this and I'm starting to wonder if this is one of those situations that the problem is right there in front of you but you've gone code blind. Ah, by the way, there's a typo on Random Terrain's chapter on rand. Here:

 

post-28085-129761342651_thumb.png

 

Any help will be greatly welcomed! :thumbsup:

random_bug1.bas

random_bug2.bas

random_ok.bas

random_bug2.bas.bin

random_ok.bas.bin

random_bug1.bas.bin

Link to comment
Share on other sites

When I add a new data to the statement and change...

 

(rand&7)

 

... into this:

 

(rand&

 

...since there's a new element in there, the code runs into a loop of two colors...

 

The trick of using the "&" operator to quickly limit a range only works for ranges that are a power-of-2.

 

So you can only use the trick with rand&1, rand&3, rand&7, rand&15, ...

 

The heart of the trick can be seen better with the binary representation of the numbers; temp1=rand&7 can also written as temp1=rand&%00000111.

 

The &%00000111 operation drops the upper 5 bits and only uses the lower 3 bits of rand, which forces the random number to only use a maximum of 7.

 

The binary representation of 8 is %00001000. Using that with & causes the lower 3 bits and the upper 4 bits to be ignored. So the random number can only be 8 or 0.

 

Clear as mud?

  • Like 1
Link to comment
Share on other sites

When I add a new data to the statement and change...

 

(rand&7)

 

... into this:

 

(rand&

 

...since there's a new element in there, the code runs into a loop of two colors...

 

The trick of using the "&" operator to quickly limit a range only works for ranges that are a power-of-2.

 

So you can only use the trick with rand&1, rand&3, rand&7, rand&15, ...

 

The heart of the trick can be seen better with the binary representation of the numbers; temp1=rand&7 can also written as temp1=rand&000111.

 

The &000111 operation drops the upper 5 bits and only uses the lower 3 bits of rand, which forces the random number to only use a maximum of 7.

 

The binary representation of 8 is 001000. Using that with & causes the lower 3 bits and the upper 4 bits to be ignored. So the random number can only be 8 or 0.

 

Clear as mud?

 

Yes, this makes total sense now! Man, I was actually using rand without fully understanting the heart of it. Once we take the decimal into binaries the whole thing comes clear. It's actually the same idea you showed me on that flickering example where you have a frame counter and set things like frame&3, etc. I just didn't realize that the two issues/ideas were alike. Thanks a lot!

Link to comment
Share on other sites

Ah, by the way, there's a typo on Random Terrain's chapter on rand.

Fixed. Speaking of AND, there is a useful chart in the "Did You Know?" box here:

 

http://www.randomterrain.com/atari-2600-memories-batari-basic-commands.html#rand

 

I copy and paste from that chart all of the time since I can't remember anything.

Link to comment
Share on other sites

Ah, by the way, there's a typo on Random Terrain's chapter on rand.

Fixed. Speaking of AND, there is a useful chart in the "Did You Know?" box here:

 

http://www.randomterrain.com/atari-2600-memories-batari-basic-commands.html#rand

 

I copy and paste from that chart all of the time since I can't remember anything.

 

Yes, I use that chart a lot too. What actually happened was that I didn't realize that the chart was showing the possible AND "&" combinations for rand. I thought they were like examples. So, when I saw a = (rand&15), I thought I could go and use something like a = (rand&16). But now I have a good grasp of what really goes on there :thumbsup:

Link to comment
Share on other sites

Yes, I use that chart a lot too. What actually happened was that I didn't realize that the chart was showing the possible AND "&" combinations for rand. I thought they were like examples. So, when I saw a = (rand&15), I thought I could go and use something like a = (rand&16). But now I have a good grasp of what really goes on there :thumbsup:

I'll see if I can figure out how to word it so it's clear that it's not just a list of examples. If you or anyone reading this has a good phrase or sentence I could use, that would be nice. I can't think of anything good right now.

Link to comment
Share on other sites

Yes, I use that chart a lot too. What actually happened was that I didn't realize that the chart was showing the possible AND "&" combinations for rand. I thought they were like examples. So, when I saw a = (rand&15), I thought I could go and use something like a = (rand&16). But now I have a good grasp of what really goes on there :thumbsup:

I'll see if I can figure out how to word it so it's clear that it's not just a list of examples. If you or anyone reading this has a good phrase or sentence I could use, that would be nice. I can't think of anything good right now.

 

Hm... taking some from what RevEng wrote and what you have on the website... something like this, maybe?

 

"Did you know?

 

You can use the "&" operator to quickly create random numbers within a certain range. It's important to notice that it only works for ranges that are a power-of-2. That being said, the possible values are listed below:"

  • Like 1
Link to comment
Share on other sites

Hm... taking some from what RevEng wrote and what you have on the website... something like this, maybe?

Thanks. I tried all kinds of variations of what you said mixed with what RevEng said and after many hours of failure, I went with this:

 

http://www.randomterrain.com/atari-2600-memories-batari-basic-commands.html#rand

If you need a quick random number within a certain range, you can use AND (&) with the numbers 1, 3, 7, 15, 31, 63, or 127 (shown in the chart below). You can read more about it in a post by RevEng at AtariAge.
  • Like 1
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...