+Random Terrain Posted May 18, 2014 Share Posted May 18, 2014 Seems like a 50 percent chance is easy to figure out:. temp5 = rand if temp5 < 128 then do something What if you want a 10 percent chance of something happening? Would this be correct:. temp5 = rand if temp5 < 25 then do something .If that is correct, then I guess this list would be too:5% = if temp5 < 12 then do something10% = if temp5 < 25 then do something15% = if temp5 < 38 then do something20% = if temp5 < 51 then do something25% = if temp5 < 64 then do something30% = if temp5 < 76 then do something35% = if temp5 < 89 then do something40% = if temp5 < 102 then do something45% = if temp5 < 115 then do something50% = if temp5 < 128 then do something55% = if temp5 < 140 then do something60% = if temp5 < 153 then do something65% = if temp5 < 166 then do something70% = if temp5 < 179 then do something75% = if temp5 < 192 then do something80% = if temp5 < 204 then do something85% = if temp5 < 217 then do something90% = if temp5 < 230 then do something95% = if temp5 < 243 then do something Is that list correct or all wrong? Thanks. Quote Link to comment Share on other sites More sharing options...
Rybags Posted May 18, 2014 Share Posted May 18, 2014 Assuming random #s coming in as values 0-255. <25 - 9.765625% chance <26 - 10.15625% chance To get %age, use <# of passing possibilities> / 256 * 100 If you're fussy and want a more exact test then you could use multiple random samples - each bit added gives that extra precision. Assuming 0-511 <51 - 9.9609375% chance - somewhat closer to 10% than the other two tests. Quote Link to comment Share on other sites More sharing options...
+Random Terrain Posted May 18, 2014 Author Share Posted May 18, 2014 Assuming random #s coming in as values 0-255. <25 - 9.765625% chance <26 - 10.15625% chance To get %age, use <# of passing possibilities> / 256 * 100 If you're fussy and want a more exact test then you could use multiple random samples - each bit added gives that extra precision. Assuming 0-511 <51 - 9.9609375% chance - somewhat closer to 10% than the other two tests. Thanks. When I put "10 percent of 256" in Google, the great beast tells me the answer is "25.6" so I guess I need to be rounding up. Quote Link to comment Share on other sites More sharing options...
bogax Posted May 18, 2014 Share Posted May 18, 2014 (edited) Assuming random #s coming in as values 0-255. <25 - 9.765625% chance <26 - 10.15625% chance To get %age, use <# of passing possibilities> / 256 * 100 If you're fussy and want a more exact test then you could use multiple random samples - each bit added gives that extra precision. Assuming 0-511 <51 - 9.9609375% chance - somewhat closer to 10% than the other two tests. Except that rand returns 1..255 Rand16 gives (something much closer to) a "perfect" distribution (returns 0..255, with 0 coming up 255 times and all others coming up 256) So if you really want to be accurate you'd probably be better using rand16 which should give something much closer to 1/256 and do it better and faster than taking rand twice. To get eg 10% you could scale the rand16 result to 0-199 then look for <20. If you want to be REALLY accurate there's a better way but it requires another variable you could do like this rem myrand carries the bias from doing mod 10 rem forward so it gets distributed rem rand & 15 is used to limit the range rem so that mod 10 can be done by subtraction myrand = (rand & 15) + myrand if myrand >= 10 then myrand = myrand - 10 if myrand >= 10 then myrand = myrand - 10 Edited May 18, 2014 by bogax Quote Link to comment Share on other sites More sharing options...
accousticguitar Posted May 18, 2014 Share Posted May 18, 2014 In my experience Rand16 does a better job of giving a truly random number. Quote Link to comment Share on other sites More sharing options...
+Random Terrain Posted May 18, 2014 Author Share Posted May 18, 2014 Except that rand returns 1..255 I thought it started at 0? Isn't that why a = (rand&1) and a = (rand/128) randomly give 0 or 1? I'm using the DPC+ kernel for the thing I'm trying to finish right now, so I don't need rand16. In my experience Rand16 does a better job of giving a truly random number. I think that's been everybody's experience. I just wonder if anything has changed since RevEng has been improving batari Basic. Doesn't seem to be much difference between the two test programs below. With regular rand: random_code_with_rand.bin With rand16: random_code_with_rand16.bin Quote Link to comment Share on other sites More sharing options...
bogax Posted May 18, 2014 Share Posted May 18, 2014 I thought it started at 0? Isn't that why a = (rand&1) and a = (rand/128) randomly give 0 or 1? I'm using the DPC+ kernel for the thing I'm trying to finish right now, so I don't need rand16. I think that's been everybody's experience. I just wonder if anything has changed since RevEng has been improving batari Basic. Doesn't seem to be much difference between the two test programs below. With regular rand: random_code_with_rand.bin With rand16: random_code_with_rand16.bin a = (rand&1) will return 0 for even numbers and 1 for odd numbers. a = (rand/128) will return 0 for numbers < 128 and 1 for numbers > 127 They both return one less 0 in a full cycle of 255 numbers so 128 1's and 127 0's for the total of 255 for rand16 it'd be 32768 1's and 32767 0's (for a full cycle of 65535) Do you have the source for your binaries somewhere so we can figure out what we're seeing? Quote Link to comment Share on other sites More sharing options...
+Random Terrain Posted May 18, 2014 Author Share Posted May 18, 2014 Do you have the source for your binaries somewhere so we can figure out what we're seeing? Yep: random_code_with_rand.bas random_code_with_rand16.bas Quote Link to comment Share on other sites More sharing options...
RevEng Posted May 18, 2014 Share Posted May 18, 2014 I just wonder if anything has changed since RevEng has been improving batari Basic. Nope. I haven't touched the rand code at all. 1 Quote Link to comment Share on other sites More sharing options...
SeaGtGruff Posted May 18, 2014 Share Posted May 18, 2014 (edited) I thought it started at 0? Isn't that why a = (rand&1) and a = (rand/128) randomly give 0 or 1? No, rand never returns 0-- unless you set it to 0, which breaks it (i.e., it will always return 0). You can get a value of 0 in your results if you're using & or / with rand, because of the way the bits are being masked (by &) or shifted (by /)-- but rand itself won't be 0 (unless you 've broken it). On the other hand, rand16 can return 0. Edit: You can also get 0 if you're using other mathematical operations with rand. For example: rand - 1 will return 0 through 254. rand + 1 will return 0 through 255 except that 1 will be skipped rand * 2 will return 0 through 254 except the odd numbers will be skipped. Edited May 18, 2014 by SeaGtGruff 1 Quote Link to comment Share on other sites More sharing options...
+Random Terrain Posted May 19, 2014 Author Share Posted May 19, 2014 On the other hand, rand16 can return 0. That's all I ever use when I'm not playing around with the DPC+ kernel, so I assumed rand was the same way. I just did 3 tests using this: . a = rand if a < 32 then pfpixel a 0 on . This one uses rand with the standard kernel: random_code_with_rand_32.bin [The zero spot never gets filled in (1-255).] This one uses rand16 with the standard kernel: random_code_with_rand16_32.bin [The zero spot gets filled in (0-255).] This one uses rand with the DPC+ kernel: random_code_with_dpc_rand_32.bin [The zero spot gets filled in (0-255).] Quote Link to comment Share on other sites More sharing options...
SeaGtGruff Posted May 19, 2014 Share Posted May 19, 2014 This one uses rand with the DPC+ kernel: random_code_with_dpc_rand_32.bin [The zero spot gets filled in (0-255).] Interesting. According to the DPCplus.h file, the DPC+ kernel uses a 32-bit LFSR for random numbers, so I guess the DPC+ version of rand could be called rand32. 1 Quote Link to comment Share on other sites More sharing options...
+Random Terrain Posted May 20, 2014 Author Share Posted May 20, 2014 In case I or anybody else need this in the future, here is the updated list with rounded up numbers: 5% = if rand < 13 then do something10% = if rand < 26 then do something15% = if rand < 38 then do something20% = if rand < 51 then do something25% = if rand < 64 then do something30% = if rand < 77 then do something35% = if rand < 90 then do something40% = if rand < 102 then do something45% = if rand < 115 then do something50% = if rand < 128 then do something55% = if rand < 141 then do something60% = if rand < 154 then do something65% = if rand < 166 then do something70% = if rand < 179 then do something75% = if rand < 192 then do something80% = if rand < 205 then do something85% = if rand < 218 then do something90% = if rand < 230 then do something95% = if rand < 243 then do something 1 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.