Jump to content

Photo

Random numbers (rand)


6 replies to this topic

#1 Muddyfunster OFFLINE  

Muddyfunster

    Space Invader

  • 12 posts

Posted Thu Sep 7, 2017 2:24 PM

Hi all,

 

I've been experimenting with random numbers and I've come to the conclusion either I'm a dumbass (entirely possible) or that random numbers are broken. I've attached a couple of .bas files to illustrate my problem:

 

score1.bas

 

This program takes a couple of variables, _cabbages and _potatoes and assigns them a value, adds them together and shows that number in the score.(100 potatoes + 50 cabbages = 150)

 

This works fine, so far so good.

 

score2.bas

 

I want to have one of the variables be a random number in stead of a defined value. (random # cabbages). I read the info on RT's site for random numbers so expected the syntax to be "a = rand" or "<var> = rand"  (http://www.randomter...mands.html#rand)

 

So instead of _cabbages = 50, I use _cabbages = rand.  I expected a random number between 0 and 255 , so my score would show 100 + whatever the random number is.

 

When I run my program, the score displays "206". Great! a random number!.

 

I run it again and it's still 206 and again, and again..It appears that for some reason, rand isn't generating a random number or I'm doing something wrong that I can't fathom. 

 

score3.bas

 

Finally I tried moving the "rand" command into the main loop and triggered it with the fire-button. This seemed to work but the random numbers + the set variable were returning values outside of the range I expected.

 

I was expecting 100 + rand, so I was really confused as to how a variable with a value of 100 + (random number between 0 & 255) can equal a number < 100.

 

I'd appreciate it if one of the more experienced hands could put me out of my random misery  :)

 

Thanks,

 

Muddy.

 

Attached Files



#2 ZackAttack OFFLINE  

ZackAttack

    Moonsweeper

  • 485 posts
  • Location:Orlando, FL US

Posted Thu Sep 7, 2017 2:36 PM

I was really confused as to how a variable with a value of 100 + (random number between 0 & 255) can equal a number < 100.

 

Sounds like you have overflowed the 8-bit variable. I.E. 100 + 160 = 266, this is bigger than fits in 8 bits so you end up with 256 less than that. It's easier to visualize in binary:

  0110 0100 = 100 decimal
+ 1010 0000 = 160 decimal
-----------
1 0000 0100 = 260 decimal
^
This 9th bit is discarded because you've done nothing with the carry flag. Effectively giving you the 8-bit number 0000 0100 in binary or 4 decimal or 260-256 decimal.

I think an easy fix would be to divide rand by a power of 2 in order to get a smaller range.



#3 Muddyfunster OFFLINE  

Muddyfunster

    Space Invader

  • Topic Starter
  • 12 posts

Posted Thu Sep 7, 2017 3:31 PM

 

Sounds like you have overflowed the 8-bit variable. I.E. 100 + 160 = 266, this is bigger than fits in 8 bits so you end up with 256 less than that. It's easier to visualize in binary:

  0110 0100 = 100 decimal
+ 1010 0000 = 160 decimal
-----------
1 0000 0100 = 260 decimal
^
This 9th bit is discarded because you've done nothing with the carry flag. Effectively giving you the 8-bit number 0000 0100 in binary or 4 decimal or 260-256 decimal.

I think an easy fix would be to divide rand by a power of 2 in order to get a smaller range.

 

Thanks for the reply. I understand what you are saying. Something like = (rand&127) would prevent the overflow. (100+ rand up to 127 = max 227).

 

So my question would be, how would I be able to use numbers greater than 255, if for example I wanted to represent 300 (0100101100)?

 

(the idea I'm working on for my game is pretty reliant on this)

 

Thanks !



#4 Random Terrain ONLINE  

Random Terrain

    Visual batari Basic User

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

Posted Thu Sep 7, 2017 4:15 PM

When I run my program, the score displays "206". Great! a random number!.
 
I run it again and it's still 206 and again, and again..It appears that for some reason, rand isn't generating a random number or I'm doing something wrong that I can't fathom.


If you put the following in your main loop instead of before it, you'll see a bunch of random numbers in the score:

 

   _potatoes = 100
   _cabbages = rand
   _total = _potatoes + _cabbages

 

Getting the same random number the first time might be an emulator problem since DPC+ random numbers are supposed to be the best you can get.



#5 ZackAttack OFFLINE  

ZackAttack

    Moonsweeper

  • 485 posts
  • Location:Orlando, FL US

Posted Fri Sep 8, 2017 8:06 AM

So my question would be, how would I be able to use numbers greater than 255, if for example I wanted to represent 300 (0100101100)?

 

You would need to create a 16bit variable out of two 8bit variables. I'm not sure if bB natively supports this or not. Perhaps someone has already created some sort of math library that allows this to be done. If not you could always do it by embedding a few lines of assembly in your bB source. I think you should look for topics related to 16bit variables and post a new one if you can't find anything.



#6 Muddyfunster OFFLINE  

Muddyfunster

    Space Invader

  • Topic Starter
  • 12 posts

Posted Fri Sep 8, 2017 8:37 AM

 

You would need to create a 16bit variable out of two 8bit variables. I'm not sure if bB natively supports this or not. Perhaps someone has already created some sort of math library that allows this to be done. If not you could always do it by embedding a few lines of assembly in your bB source. I think you should look for topics related to 16bit variables and post a new one if you can't find anything.

Thanks Zack,

 

I'll look in to that later today and see what a search turns up.

 

Thanks for the replies everyone.



#7 CapitanClassic OFFLINE  

CapitanClassic

    Dragonstomper

  • 511 posts

Posted Fri Sep 8, 2017 12:32 PM

Here is something for the Commodore 64, they have a Random Number section with 8bit and 16bit periods.
http://codebase64.or...6502_6510_maths




0 user(s) are browsing this forum

0 members, 0 guests, 0 anonymous users