Probabilistic choice: Difference between revisions

Added Easylang
(→‎{{header|jq}}: simplify align_decimal)
(Added Easylang)
 
(7 intermediate revisions by 3 users not shown)
Line 942:
stdout.print(item, "\t", timesFound[item] / trials, "\t", probTable[item], "\n")
}</syntaxhighlight>
 
=={{header|EasyLang}}==
<syntaxhighlight>
name$[] = [ "aleph " "beth " "gimel " "daleth" "he " "waw " "zayin " "heth " ]
probs[] = [ 1 / 5 1 / 6 1 / 7 1 / 8 1 / 9 1 / 10 1 / 11 0 ]
for i = 1 to 7
cum += probs[i]
cum[] &= cum
.
cum[] &= 1
probs[8] = 1 - cum[7]
len act[] 8
n = 1000000
for i to n
h = randomf
j = 1
while h > cum[j]
j += 1
.
act[j] += 1
.
print "Name Ratio Expected"
print "---------------------"
numfmt 4 6
for i to 8
print name$[i] & " " & act[i] / n & " " & probs[i]
.
</syntaxhighlight>
 
{{out}}
<pre>
Name Ratio Expected
---------------------
aleph 0.2000 0.2000
beth 0.1661 0.1667
gimel 0.1424 0.1429
daleth 0.1252 0.1250
he 0.1111 0.1111
waw 0.1003 0.1000
zayin 0.0913 0.0909
heth 0.0637 0.0635
</pre>
 
=={{header|Elixir}}==
Line 1,435 ⟶ 1,477:
he 0.110772 0.111111
waw 0.100236 0.100000
zayin> 0.090664 0.090909
heth 0.063412 0.063456
-------- --------
1.000000 1.000000
</pre>
 
 
 
=={{header|FutureBasic}}==
<syntaxhighlight lang="futurebasic">
_elements = 8
 
local fn ProbabilisticChoice
double prob(_elements), cumulative(_elements)
Str15 item(_elements)
double r, p, sum = 0, checksum = 0
long i, j, samples = 1000000
item(1) = "aleph" : item(2) = "beth" : item(3) = "gimel" : item(4) = "daleth"
item(5) = "he" : item(6) = "waw" : item(7) = "zayin" : item(8) = "heth"
prob(1) = 1/5.0 : prob(2) = 1/6.0 : prob(3) = 1/7.0 : prob(4) = 1/8.0
prob(5) = 1/9.0 : prob(6) = 1/10.0 : prob(7) = 1/11.0 : prob(8) = 1759/27720
for i = 1 to _elements
sum += prob(i)
next
if abs(sum-1) > samples then print "Probabilities don't sum to 1." : exit fn
for i = 1 to samples
cln r = (((double)arc4random()/0x100000000));
p = 0
for j = 1 to _elements
p += prob(j)
if (r < p) then cumulative(j) += 1 : exit for
next
next
print
printf @"Item Actual Theoretical"
printf @"---- ------ -----------"
for i = 1 to _elements
printf @"%-7s %10.6f %12.6f", item(i), cumulative(i)/samples, prob(i)
checksum += cumulative(i)/samples
next
printf @" -------- -----------"
printf @"%17.6f %12.6f", checksum, 1.000000
end fn
 
fn ProbabilisticChoice
 
HandleEvents
</syntaxhighlight>
{{output}}
<pre>
Item Actual Theoretical
---- ------ -----------
aleph 0.199966 0.200000
beth 0.166505 0.166667
gimel 0.142958 0.142857
daleth 0.124827 0.125000
he 0.111451 0.111111
waw 0.100095 0.100000
zayin 0.090985 0.090909
heth 0.063213 0.063456
-------- -----------
1.000000 1.000000
</pre>
 
Line 3,890 ⟶ 3,995:
heth 0.068800 0.063456 0.005344
</pre>
 
=={{header|SparForte}}==
As a structured script.
<syntaxhighlight lang="ada">#!/usr/local/bin/spar
pragma annotate( summary, "randdist" )
@( description, "Given a mapping between items and their required" )
@( description, "probability of occurrence, generate a million items" )
@( description, "randomly subject to the given probabilities and compare" )
@( description, "the target probability of occurrence versus the" )
@( description, "generated values." )
@( description, "" )
@( description, "The total of all the probabilities should equal one." )
@( description, "(Because floating point arithmetic is involved this is" )
@( description, "subject to rounding errors). Use the following mapping" )
@( description, "to test your programs: aleph 1/5.0, beth 1/6.0," )
@( description, "gimel 1/7.0, daleth 1/8.0, he 1/9.0, waw 1/10.0" )
@( description, "zayin 1/11.0, heth 1759/27720 adjusted so that" )
@( description, "probabilities add to 1" )
@( see_also, "http://rosettacode.org/wiki/Probabilistic_choice" )
@( author, "Ken O. Burtch" );
pragma license( unrestricted );
 
pragma restriction( no_external_commands );
 
procedure randdist is
trials : constant positive := 1_000_000;
type outcome is (aleph, beth, gimel, daleth, he, waw, zayin, heth);
pr : constant array(aleph..heth) of float :=
(1/5, 1/6, 1/7, 1/8, 1/9, 1/10, 1/11, 1 );
samples : array(aleph..heth) of natural := (0, 0, 0, 0, 0, 0, 0, 0);
random_value : float;
begin
for try in 1..trials loop
random_value := numerics.random;
for i in arrays.first( pr )..arrays.last( pr ) loop
if random_value <= pr(i) then
samples(i) := samples(i) + 1;
exit;
else
random_value := @ - pr(i);
end if;
end loop;
end loop;
-- Show results
for i in arrays.first( pr )..arrays.last( pr ) loop
put( i ) @ ( " " ) @ ( float( samples( i ) ) / float( trials ) );
if i = heth then
put_line( " rest" );
else
put_line( pr(i) );
end if;
end loop;
end randdist;</syntaxhighlight>
{{out}}
<pre>
$ spar randdist
aleph 2.00260000000000E-01 2.00000000000000E-01
beth 1.66376000000000E-01 1.66666666666667E-01
gimel 1.42698000000000E-01 1.42857142857143E-01
daleth 1.25408000000000E-01 1.25000000000000E-01
he 1.11311000000000E-01 1.11111111111111E-01
waw 9.97980000000000E-02 1.00000000000000E-01
zayin 9.09570000000000E-02 9.09090909090909E-02
heth 6.31920000000000E-02 rest</pre>
 
=={{header|Stata}}==
Line 4,038 ⟶ 4,207:
{{trans|Kotlin}}
{{libheader|Wren-fmt}}
<syntaxhighlight lang="ecmascriptwren">import "random" for Random
import "./fmt" for Fmt
 
var letters = ["aleph", "beth", "gimel", "daleth", "he", "waw", "zayin", "heth"]
1,973

edits