Page 1 of 1

creating a distrubution

Posted: Fri Apr 24, 2009 3:20 am
by ac11ca
Hi, I would like to create a variable that contains a distribution of values characterised by: 80% of values in the distribution are "4" and 20% of the values are "0". When the user clicks on a button I would like the button label to be replaced with either "4" or "0" in the likihoods specificed by the distribution (i.e., on 80% of button presses "4" will appear, and on 20% of button presses "0" will appear).

The way I have been doing this up until now is to create a fld with 100 numbers, in the correct proportion to the ratio I want, and then randomising the list, and then selecting the first value from the list. This method is time consuming and I was wondering if there was a better, quicker way?

Cheers,
Adrian

Posted: Fri Apr 24, 2009 12:18 pm
by BvG
the easiest way is to find the one number that can divide both your target numbers without overflow. as you got percentages this is especially simple:

80 / 10 / 2 = 4
20 / 10 / 2 = 1

so your target ratio is 4:1. For using a ratio with randomising, you need to add both numbers (4+1=5), that will be the number you want in your code:

Code: Select all

on mouseUp
  put random(5) into theNumber
  if theNumber > 1 then
    --it's 2-5
    put 4
  else
    --it's 1
    put 1
  end if
end mouseUp
of course you can also take your percentage based system directly (20+80 = 100):

Code: Select all

on mouseUp
  put random(100) into theNumber
  if theNumber > 20 then
    --21-80%
    put 4
  else
    --1-20%
    put 1
  end if
end mouseUp

Posted: Sat Apr 25, 2009 5:54 am
by ac11ca
Thanks BvG - that is a clever method (that I wish I had thought of last year!).

Cheers,
Adrian