Probabilistic choice: Difference between revisions
Content added Content deleted
(Updated to work with Nim 1.4. Heavy restructuring.) |
(Added AppleScript.) |
||
Line 151: | Line 151: | ||
Attained prob.: 0.199987 0.166917 0.142531 0.124203 0.111338 0.099702 0.091660 0.063662 |
Attained prob.: 0.199987 0.166917 0.142531 0.124203 0.111338 0.099702 0.091660 0.063662 |
||
</pre> |
</pre> |
||
=={{header|AppleScript}}== |
|||
AppleScript does have a <tt>random number</tt> command, but this is located in the StandardAdditions OSAX and invoking it a million times can take quite a while. Since Mac OS X 10.11, it's been possible to use the randomising features of the system's "GameplayKit" framework, which are faster to access. |
|||
<lang applescript>use AppleScript version "2.5" -- Mac OS X 10.11 (El Capitan) or later. |
|||
use framework "Foundation" |
|||
use framework "GameplayKit" |
|||
on probabilisticChoices(mapping, picks) |
|||
script o |
|||
property mapping : {} |
|||
end script |
|||
-- Make versions of the mapping records with additional 'actual' properties … |
|||
set mapping to current application's class "NSMutableArray"'s arrayWithArray:(mapping) |
|||
tell mapping to makeObjectsPerformSelector:("addEntriesFromDictionary:") withObject:({actual:0}) |
|||
-- … ensuring that they're sorted (for accuracy) in descending order (for efficiency) of probability. |
|||
set descriptor to current application's class "NSSortDescriptor"'s sortDescriptorWithKey:("probability") ascending:(false) |
|||
tell mapping to sortUsingDescriptors:({descriptor}) |
|||
set o's mapping to mapping as list |
|||
set rndGenerator to current application's class "GKRandomDistribution"'s distributionForDieWithSideCount:(picks) |
|||
set onePickth to 1 / picks |
|||
repeat picks times |
|||
-- Get a random number between 0.0 and 1.0. |
|||
set r to rndGenerator's nextUniform() |
|||
-- Interpret the probability of the number occurring in the range it does |
|||
-- as picking the item with the same probability of being picked. |
|||
repeat with thisRecord in o's mapping |
|||
set r to r - (thisRecord's probability) |
|||
if (r ≤ 0) then |
|||
set thisRecord's actual to (thisRecord's actual) + onePickth |
|||
exit repeat |
|||
end if |
|||
end repeat |
|||
end repeat |
|||
return o's mapping |
|||
end probabilisticChoices |
|||
on task() |
|||
set mapping to {{|item|:"aleph", probability:1 / 5}, {|item|:"beth", probability:1 / 6}, ¬ |
|||
{|item|:"gimel", probability:1 / 7}, {|item|:"daleth", probability:1 / 8}, ¬ |
|||
{|item|:"he", probability:1 / 9}, {|item|:"waw", probability:1 / 10}, ¬ |
|||
{|item|:"zayin", probability:1 / 11}, {|item|:"heth", probability:1759 / 27720}} |
|||
set picks to 1000000 |
|||
set theResults to probabilisticChoices(mapping, picks) |
|||
set output to {} |
|||
set template to {"|item|:", missing value, ", probability:", missing value, ", actual:", missing value} |
|||
set astid to AppleScript's text item delimiters |
|||
set AppleScript's text item delimiters to "" |
|||
repeat with thisRecord in theResults |
|||
set {|item|:item 2 of template, probability:item 4 of template, actual:item 6 of template} to thisRecord |
|||
set {|item|:template's second item, probability:template's fourth item, actual:template's sixth item} to thisRecord |
|||
set end of output to template as text |
|||
end repeat |
|||
set AppleScript's text item delimiters to "}, ¬ |
|||
{" |
|||
set output to "{ ¬ |
|||
{" & output & "} ¬ |
|||
}" |
|||
set AppleScript's text item delimiters to astid |
|||
return output |
|||
end task |
|||
task()</lang> |
|||
{{output}} |
|||
<lang applescript>"{ ¬ |
|||
{|item|:aleph, probability:0.2, actual:0.20033}, ¬ |
|||
{|item|:beth, probability:0.166666666667, actual:0.166744}, ¬ |
|||
{|item|:gimel, probability:0.142857142857, actual:0.142403}, ¬ |
|||
{|item|:daleth, probability:0.125, actual:0.125195}, ¬ |
|||
{|item|:he, probability:0.111111111111, actual:0.110284}, ¬ |
|||
{|item|:waw, probability:0.1, actual:0.100505}, ¬ |
|||
{|item|:zayin, probability:0.090909090909, actual:0.090721}, ¬ |
|||
{|item|:heth, probability:0.063455988456, actual:0.063817} ¬ |
|||
}"</lang> |
|||
=={{header|AutoHotkey}}== |
=={{header|AutoHotkey}}== |