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
creating a distrubution
Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller
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:
of course you can also take your percentage based system directly (20+80 = 100):
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
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
Various teststacks and stuff:
http://bjoernke.com
Chat with other RunRev developers:
chat.freenode.net:6666 #livecode
http://bjoernke.com
Chat with other RunRev developers:
chat.freenode.net:6666 #livecode