Jump to content

Photo

Random Numbers + Data Statements


9 replies to this topic

#1 diogoandrei OFFLINE  

diogoandrei

    Chopper Commander

  • 211 posts
  • Location:Brazil

Posted Sun Feb 13, 2011 10:14 AM

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&<img src='http://www.atariage.com/forums/public/style_emoticons/<#EMO_DIR#>/icon_cool.gif' class='bbc_emoticon' alt='8)' />

...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&<img src='http://www.atariage.com/forums/public/style_emoticons/<#EMO_DIR#>/icon_cool.gif' class='bbc_emoticon' alt='8)' />
	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:

RT_psuedo.png

Any help will be greatly welcomed! :thumbsup:

Attached Files



#2 RevEng OFFLINE  

RevEng

    Quadrunner

  • 5,020 posts
  • Bitnik
  • Location:bottom of the stack

Posted Sun Feb 13, 2011 10:50 AM

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

(rand&7)

... into this:

(rand&<img src='http://www.atariage.com/forums/public/style_emoticons/<#EMO_DIR#>/icon_cool.gif' class='bbc_emoticon' alt='8)' />

...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?

#3 diogoandrei OFFLINE  

diogoandrei

    Chopper Commander

  • Topic Starter
  • 211 posts
  • Location:Brazil

Posted Sun Feb 13, 2011 11:13 AM

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

(rand&7)

... into this:

(rand&<img src='http://www.atariage.com/forums/public/style_emoticons/<#EMO_DIR#>/icon_cool.gif' class='bbc_emoticon' alt='8)' />

...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!

#4 RevEng OFFLINE  

RevEng

    Quadrunner

  • 5,020 posts
  • Bitnik
  • Location:bottom of the stack

Posted Sun Feb 13, 2011 11:40 AM

Glad it helped!

#5 Random Terrain OFFLINE  

Random Terrain

    Visual batari Basic User

  • 28,799 posts
  • Controlled Randomness
    Replay Value
    Nonlinear
  • Location:North Carolina (USA)

Posted Sun Feb 13, 2011 2:07 PM

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.randomter...mands.html#rand

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

#6 diogoandrei OFFLINE  

diogoandrei

    Chopper Commander

  • Topic Starter
  • 211 posts
  • Location:Brazil

Posted Sun Feb 13, 2011 2:20 PM

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.randomter...mands.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:

#7 Random Terrain OFFLINE  

Random Terrain

    Visual batari Basic User

  • 28,799 posts
  • Controlled Randomness
    Replay Value
    Nonlinear
  • Location:North Carolina (USA)

Posted Sun Feb 13, 2011 5:23 PM

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.

#8 diogoandrei OFFLINE  

diogoandrei

    Chopper Commander

  • Topic Starter
  • 211 posts
  • Location:Brazil

Posted Sun Feb 13, 2011 7:44 PM

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:"


#9 Random Terrain OFFLINE  

Random Terrain

    Visual batari Basic User

  • 28,799 posts
  • Controlled Randomness
    Replay Value
    Nonlinear
  • Location:North Carolina (USA)

Posted Mon Feb 14, 2011 2:43 AM

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.randomter...mands.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.



#10 diogoandrei OFFLINE  

diogoandrei

    Chopper Commander

  • Topic Starter
  • 211 posts
  • Location:Brazil

Posted Wed Feb 16, 2011 9:34 PM

Nice :thumbsup:




0 user(s) are browsing this forum

0 members, 0 guests, 0 anonymous users