+Gemintronic Posted June 28, 2010 Share Posted June 28, 2010 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. Quote Link to comment Share on other sites More sharing options...
yuppicide Posted June 28, 2010 Share Posted June 28, 2010 (edited) What I was doing is like this: routinename h = rand if h > 29 then goto routinename I think there was an easier way. I am at work right now and can't find the post. I believe RevEng shows me how to do it. Edited June 28, 2010 by yuppicide Quote Link to comment Share on other sites More sharing options...
jrok Posted June 28, 2010 Share Posted June 28, 2010 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. Quote Link to comment Share on other sites More sharing options...
+Gemintronic Posted June 28, 2010 Author Share Posted June 28, 2010 (edited) 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 June 28, 2010 by theloon Quote Link to comment Share on other sites More sharing options...
GroovyBee Posted June 28, 2010 Share Posted June 28, 2010 What do you need the number for? What sort of decisions using "if" statements do you intend to use? Quote Link to comment Share on other sites More sharing options...
yuppicide Posted June 28, 2010 Share Posted June 28, 2010 (edited) 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 June 28, 2010 by yuppicide Quote Link to comment Share on other sites More sharing options...
+Gemintronic Posted June 28, 2010 Author Share Posted June 28, 2010 (edited) 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 Edited June 28, 2010 by theloon Quote Link to comment Share on other sites More sharing options...
GroovyBee Posted June 28, 2010 Share Posted June 28, 2010 This might help :- http://www.atariage.com/forums/topic/151073-random-placement/ Quote Link to comment Share on other sites More sharing options...
jrok Posted June 28, 2010 Share Posted June 28, 2010 (edited) 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 June 29, 2010 by jrok Quote Link to comment Share on other sites More sharing options...
yuppicide Posted June 28, 2010 Share Posted June 28, 2010 This post looks good: http://www.atariage.com/forums/topic/151073-random-placement/page__view__findpost__p__1846750 Quote Link to comment Share on other sites More sharing options...
RevEng Posted June 29, 2010 Share Posted June 29, 2010 ... 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. Quote Link to comment Share on other sites More sharing options...
+atari2600land Posted June 29, 2010 Share Posted June 29, 2010 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. Quote Link to comment Share on other sites More sharing options...
yuppicide Posted June 29, 2010 Share Posted June 29, 2010 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. Quote Link to comment Share on other sites More sharing options...
+atari2600land Posted June 29, 2010 Share Posted June 29, 2010 you can do others, right? a=(rand/+1 = 32 and /16 = 16? Yep. That's right. You can also do /32+1 to get a number between 1 & 8, /64+1 to get a number between 1 & 4, and /128+1 to get either 1 or 2. Quote Link to comment Share on other sites More sharing options...
yuppicide Posted June 29, 2010 Share Posted June 29, 2010 I sent this to RT to add to the BB page. Very useful information. Quote Link to comment Share on other sites More sharing options...
+Gemintronic Posted June 29, 2010 Author Share Posted June 29, 2010 (edited) 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. 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 June 29, 2010 by theloon Quote Link to comment Share on other sites More sharing options...
accousticguitar Posted June 29, 2010 Share Posted June 29, 2010 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. Quote Link to comment Share on other sites More sharing options...
+Gemintronic Posted June 29, 2010 Author Share Posted June 29, 2010 (edited) 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 June 29, 2010 by theloon Quote Link to comment Share on other sites More sharing options...
GroovyBee Posted June 29, 2010 Share Posted June 29, 2010 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). Quote Link to comment Share on other sites More sharing options...
+Random Terrain Posted June 30, 2010 Share Posted June 30, 2010 a=(rand/4)+1would give you a number between 1 and 64. If I'm using this correctly, a=(rand&63)+1 is two cycles faster than a=(rand/4)+1. Maybe somebody else could test it to be sure. Quote Link to comment Share on other sites More sharing options...
Nukey Shay Posted June 30, 2010 Share Posted June 30, 2010 It should be 2 cycles faster. AND #$3F CLC ADC #$01 vs: LSR LSR CLC ADC #$01 Quote Link to comment Share on other sites More sharing options...
+Random Terrain Posted June 30, 2010 Share Posted June 30, 2010 It should be 2 cycles faster. AND #$3F CLC ADC #$01 vs: LSR LSR CLC ADC #$01 Thanks. I guess it would be safe to put this info on the bB page then. Quote Link to comment Share on other sites More sharing options...
+Random Terrain Posted June 30, 2010 Share Posted June 30, 2010 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. Quote Link to comment Share on other sites More sharing options...
Omegamatrix Posted June 30, 2010 Share Posted June 30, 2010 It should be 2 cycles faster. AND #$3F CLC ADC #$01 If you don't mind illegal ops, and having the result in the X register... it can be done in 4 cycles and 4 bytes. LDX #$3F SBX #$FF Value starts in A, and ends up in X. Quote Link to comment Share on other sites More sharing options...
Nukey Shay Posted June 30, 2010 Share Posted June 30, 2010 I was commenting on where the 2-cycle difference between methods probably comes from in bB Quote Link to comment Share on other sites More sharing options...
Recommended Posts
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.