Jump to content
IGNORED

ranged random numbers


bogax

Recommended Posts

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 by bogax
  • Like 1
Link to comment
Share on other sites

  • 1 year later...

Here I've thrown together some script
to 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 any
other 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 the
variable to use.

So if you have a lower limit of 5 and a
range of 15 you should get numbers from
5..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 of
room so it actually generates bB code for
range + 1 which in effect rounds down to the
given range.

Looks like it should work but I haven't fully
tested it.

The asm is more accurate and gives a more
uniform distribution but both bB and asm
are more uniform given bB rand function than
actual random numbers would be.

There are other ways to do this
This code just scales the result of the rand
function to the desired range.

If the range is a power of 2 an AND mask is
obviously better (shorter, faster).

 

ranged_rand.html

 

 

warning you get a splash ad you have to click through

Edited by bogax
  • Like 1
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...