creating a distrubution

LiveCode is the premier environment for creating multi-platform solutions for all major operating systems - Windows, Mac OS X, Linux, the Web, Server environments and Mobile platforms. Brand new to LiveCode? Welcome!

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

Post Reply
ac11ca
Posts: 41
Joined: Sun Mar 16, 2008 2:22 am

creating a distrubution

Post by ac11ca » Fri Apr 24, 2009 3:20 am

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

BvG
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 1239
Joined: Sat Apr 08, 2006 1:10 pm
Contact:

Post by BvG » Fri Apr 24, 2009 12:18 pm

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
Various teststacks and stuff:
http://bjoernke.com

Chat with other RunRev developers:
chat.freenode.net:6666 #livecode

ac11ca
Posts: 41
Joined: Sun Mar 16, 2008 2:22 am

Post by ac11ca » Sat Apr 25, 2009 5:54 am

Thanks BvG - that is a clever method (that I wish I had thought of last year!).

Cheers,
Adrian

Post Reply