Jump to content
IGNORED

Random numbers (rand)


Muddyfunster

Recommended Posts

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.randomterrain.com/atari-2600-memories-batari-basic-commands.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.

 

score1.bas

score2.bas

score3.bas

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

 

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 !

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

 

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.

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