How to resolve the algorithm Probabilistic choice step by step in the Raku programming language
Published on 12 May 2024 09:40 PM
How to resolve the algorithm Probabilistic choice step by step in the Raku programming language
Table of Contents
Problem Statement
Given a mapping between items and their required probability of occurrence, generate a million items randomly subject to the given probabilities and compare the target probability of occurrence versus the generated values. The total of all the probabilities should equal one. (Because floating point arithmetic is involved, this is subject to rounding errors).
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Probabilistic choice step by step in the Raku programming language
Source code in the raku programming language
constant TRIALS = 1e6;
constant @event = ;
constant @P = flat (1 X/ 5 .. 11), 1759/27720;
constant @cP = [\+] @P;
my atomicint @results[+@event];
(^TRIALS).race.map: { @results[ @cP.first: { $_ > once rand }, :k ]⚛++; }
say 'Event Occurred Expected Difference';
for ^@results {
my ($occurred, $expected) = @results[$_], @P[$_] * TRIALS;
printf "%-9s%8.0f%9.1f%12.1f\n",
@event[$_],
$occurred,
$expected,
abs $occurred - $expected;
}
You may also check:How to resolve the algorithm Two bullet roulette step by step in the Perl programming language
You may also check:How to resolve the algorithm Taxicab numbers step by step in the PureBasic programming language
You may also check:How to resolve the algorithm Determine if two triangles overlap step by step in the Groovy programming language
You may also check:How to resolve the algorithm Tokenize a string step by step in the TUSCRIPT programming language
You may also check:How to resolve the algorithm Integer overflow step by step in the M2000 Interpreter programming language