bogax Posted March 9, 2013 Share Posted March 9, 2013 (edited) Here's some code to do ranged random numbers with out the biases of some of the other methods and using only one call to rand. But it does need a dedicated variable (called my_seed here) basically it's just mod but carrying the bias forward so it gets distributed. The code is as yet UNTESTED There are two cases. If the range is between 0 - 128 and 0 - 254 (inclusive) the difference between 256 and the modulus is added back in and my_seed is taken mod the modulus (by subtraction) It uses a temp variable and needs two constants the modulus, rnd_modulus and the difference 256-modulus, mod _dif so for a range 0-137 const rnd_modulus =138 and const mod_dif = 118 temp1 = my_seed my_seed = rand + temp1 if my_seed < temp1 then my_seed = my_seed + mod_dif if my_seed >= rnd_modulus then my_seed = my_seed - rnd_modulus In asm you only need the modulus constant and my_seed. you don't need the temp variable or to have mod_dif defined as a constant asm jsr randomize clc adc my_seed bcc *+4 adc #255-rnd_modulus cmp #rnd_modulus bcc *+4 sbc #rnd_modulus sta my_seed end For a range less than 128 the result of rand is taken mod a power of two to minimize the modulus operation. It needs a temp variable and my_seed and two constants, rnd_modulus, and a power of two mask, rnd_mask, equal to the lowest power of two that's greater than the modulus, minus 1 so for a range 0-17 the next power of two is 32 and the mask is const rnd_mask = 32 - 1 and the modulus const rnd_modulus = 18 dim my_seed = variable const rnd_mask = const rnd_modulus = my_seed = (rand & rnd_mask) + my_seed if my_seed >= rnd_modulus then my_seed = my_seed - rnd_modulus if my_seed >= rnd_modulus then my_seed = my_seed - rnd_modulus in asm asm jsr randomize and #rnd_mask clc adc my_seed cmp #rnd_modulus bcc *+4 sbc #rnd_modulus cmp #rnd_modulus bcc *+4 sbc #rnd_modulus sta my_seed end Edited May 18, 2013 by bogax 1 Quote Link to comment Share on other sites More sharing options...
bogax Posted March 20, 2014 Author Share Posted March 20, 2014 (edited) Here I've thrown together some scriptto generate code for ranged random numbers.It's not fully tested.I've only tried it in Opera.I'd be curious how/if it works for anyother browser.The number on the left is the lower end.The number on the right is the range.The text on the right is the name of thevariable to use.So if you have a lower limit of 5 and arange of 15 you should get numbers from5..14 inclusive.Both asm and bB code are generated.Generally the asm is the same as the bB code.For ranges over 128 the bB code runs out ofroom so it actually generates bB code forrange + 1 which in effect rounds down to thegiven range.Looks like it should work but I haven't fullytested it.The asm is more accurate and gives a moreuniform distribution but both bB and asmare more uniform given bB rand function thanactual random numbers would be.There are other ways to do thisThis code just scales the result of the randfunction to the desired range.If the range is a power of 2 an AND mask isobviously better (shorter, faster). ranged_rand.html warning you get a splash ad you have to click through Edited March 20, 2014 by bogax 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.