Jump to content
IGNORED

Random Within Range


Gemintronic

Recommended Posts

I remember using something like "INT(199 * RND(1)))" to get a result within 1 to 199 in QuickBASIC. Is there an approximation in Batari?

 

Maybe there's a way using modulus. Something like "rand MOD 199"

 

Any clues guys? I've done a search with some hits but nothing clear.

 

 

No, but you can write a function that does something similiar.

Link to comment
Share on other sites

I remember using something like "INT(199 * RND(1)))" to get a result within 1 to 199 in QuickBASIC. Is there an approximation in Batari?

 

Maybe there's a way using modulus. Something like "rand MOD 199"

 

Any clues guys? I've done a search with some hits but nothing clear.

 

 

No, but you can write a function that does something similiar.

 

Thanks yuppicide and jrok for your ideas!

 

So far I've seen the keep-repeating-rand style and its variant halve-the-value-until-within-range. My stop-gap solution is to just use closest match evenly divisible values (i.e. rand/2, rand/4 etc)

 

@jrok: If you've got a trick up your sleeve to make a QuickBASIC conversion of RND I'd like to see it! Frankly I'm not sure I've got your chops in either QuickBASIC or Batari to even consider it. If you've got example code I'm sure it'd make alot of newbies shoot pellets out their shorts in glee :)

Edited by theloon
Link to comment
Share on other sites

From what I remember it was like:

 

h = rand (& 26)

 

But I can't find the darn post. I'm searching the website while nobody is looking since I am at work... plus most of my game code is at home. Of course you'd replace 26 with the number you want up to.. not sure if that was it or what.

Edited by yuppicide
Link to comment
Share on other sites

What do you need the number for? What sort of decisions using "if" statements do you intend to use?

 

Thanks for the reply!

 

I was thinking of the usual stuff.

 

"If a random value is 3 out of a possible 41 then change enemy AI tactics"

"If a random value is 1 out of a possible 195 then the treasure chest has a golden sword"

 

In Batari specific terms I was forced to use:

if rand = 255 then gosub change_tub

 

What I would like is to return a value within a different range than just 255. Say, change the direction if I get a 1 out of 44.

 

@Yuppiecide: Much respect for looking. I'll keep searching through RevEngs posts. Maybe I'll get lucky. Also, I'll study your provided example until I understand what h = rand (& 26) means :P

Edited by theloon
Link to comment
Share on other sites

Hmm, I dug this up from an old source file. I'm pretty sure SeaGTGruff wrote it originally:

 

   function get_rand
  if temp1 = temp2 then return temp1
  if temp1 < temp2 then temp3 = temp1 : temp4 = temp2 else temp3 = temp2 : temp4 = temp1
  temp4 = temp4 - temp3 + 1
  if temp4 = 0 then temp3 = 1 : temp4 = 255
  temp5 = 255 / temp4
  temp5 = temp4 * temp5
get_rand_loop1
  temp6 = rand
  if temp6 > temp5 then get_rand_loop1
get_rand_loop2
  if temp6 > temp4 then temp6 = temp6 - temp4 : goto get_rand_loop2
  temp6 = temp6 + temp3 - 1
  return temp6
end

 

Then you should be able to call it with statements like:

 

if...then x = get_rand(0,100)

(Number between 0 and 100)

if...then x = get_rand(6,50)

(Number between 6 and 50)

etc...

 

Of course, I'm not sure if this is as fast or as flexible as what you are looking for.

 

EDT: That syntax was a little malformed in the calls. Basically, a designated variable would store the result of the function.

Edited by jrok
Link to comment
Share on other sites

...

I was thinking of the usual stuff.

 

"If a random value is 3 out of a possible 41 then change enemy AI tactics"

"If a random value is 1 out of a possible 195 then the treasure chest has a golden sword"

...

For this kind of stuff you don't need anything except the usual random number in the 1-255 range that bB gives you. You just need to scale your odds first to fit within 1-255

 

3/41 odds are very close to 18/255, so instead you can use...

"If a random value is 18 out of a possible 255 then change enemy AI tactics"

 

There will still be times when you'll need to scale random numbers, but there's no sense in wasting cycles when you don't need to.

Link to comment
Share on other sites

Nice... that should be on RT's page.

 

So, you can do others, right? a=(rand/8)+1 = 32 and /16 = 16?

 

If you don't mind using powers of 2, you could do the following:

 

a=(rand/2)+1

would give you a number between 1 and 128, where as

a=(rand/4)+1

would give you a number between 1 and 64.

Link to comment
Share on other sites

You guys just plain rock!! Thanks for sharing the brain-power. I was getting stuck on what I want and not what I can do. :P

 

To sum up, either:

 

Use the closest matching number of possibilities

a=(rand/2)+1 (a number between 1 and 128)
a=(rand/4)+1 (a number between 1 and 64)
a=(rand/8)+1 (a number between 1 and 32)
a=(rand/16)+1 (a number between 1 and 16)
a=(rand/32)+1 (a number between 1 and 
a=(rand/64)+1 (a number between 1 and 4)

 

..or just adjust your thinking as RevEng did to make your desired result a percentage of 255.

I bet a 10 percent chance would be something like

if rand < 25 then gosub massive_damage

 

jroks code (see above for details) actually does what I originally intended so bonus points there! I must remember that cycles are precious on the 2600 so it should be used sparingly.

Edited by theloon
Link to comment
Share on other sites

Here's an idea I got from batari. You can run something like this every frame.

 

 y=rand
 if y > 16 && y < 134 then m=y

 

A random number is picked every frame. If the number falls within certain parameters (between 16 and 134 in this case) it is stored in ram. If not, the number in ram is the last number that was stored successfully. I find this is a good way to get a truly random number because the number you get depends on when (the exact millisecond) you call on that ram address.

Link to comment
Share on other sites

I already consider this more than answered but another thought draws near!

 

Do 'yall think there's a method that uses modulus to keep the value within desired bounds? I heard adding a value that would total more than 255 simply wraps around so 255 + 10 would be 9 or 10ish right?

 

Would there be a method like rand + value that would do the job?

 

Maybe

 

a = rand

if a > 128 then a = a + 128

 

..crazy, half-thought out concepts I know :)

 

@GroovyBee: Thanks for the reality check :)

Edited by theloon
Link to comment
Share on other sites

Do 'yall think there's a method that uses modulus to keep the value within desired bounds?

 

For non powers of 2, modulus=division=slow on 6502 so its best avoided.

 

I heard adding a value that would total more than 255 simply wraps around so 255 + 10 would be 9 or 10ish right?

 

255+10 is 9 and carry will be set.

 

Would there be a method like rand + value that would do the job?

 

All this will do is ensure that the lowest number produced is value when the following is true random<(255-value).

Link to comment
Share on other sites

I sent this to RT to add to the BB page. Very useful information.

See if this is OK:

 

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

 

You may need to refresh the page or clear your cache to see the new Did You Know? box.

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