Probabilistic choice: Difference between revisions

Content added Content deleted
No edit summary
(Major rewrite to simplify code)
Line 1,445: Line 1,445:
=={{header|FutureBasic}}==
=={{header|FutureBasic}}==
<syntaxhighlight lang="futurebasic">
<syntaxhighlight lang="futurebasic">
_arrItems = 7
_elements = 8


local fn ProbablisticChoice
local fn ProbabilisticChoice
double prob(_elements), cumulative(_elements)
CFArrayRef items = @[@"aleph",@"beth",@"gimel",@"daleth",@"he",@"waw",@"zayin",@"heth"]
Str15 item(_elements)
CFArrayRef prob = @[¬
double r, p, sum = 0, checksum = 0
fn NumberWithDouble(1/5.0),¬
long i, j, samples = 1000000
fn NumberWithDouble(1/6.0),¬
fn NumberWithDouble(1/7.0),¬
fn NumberWithDouble(1/8.0),¬
fn NumberWithDouble(1/9.0),¬
fn NumberWithDouble(01/10.0),¬
fn NumberWithDouble(1/11.0),¬
fn NumberWithDouble(1759/27720)]
item(1) = "aleph" : item(2) = "beth" : item(3) = "gimel" : item(4) = "daleth"
long trial, i, n = 1000000
item(5) = "he" : item(6) = "waw" : item(7) = "zayin" : item(8) = "heth"
double p, r, sum = 0, cnt(_arrItems), sumActual = 0
for i = 0 to len(prob)-1
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
sum = sum + fn NumberDoubleValue( prob[i] )
for i = 1 to 8
sum += prob(i)
next
next
if ( sum != 1 ) then print "Probabilities don't sum to 1" : exit fn
if abs(sum-1) > samples then print "Probabilities don't sum to 1." : exit fn
for trial = 1 to n
for i = 1 to samples
cln r = (((double)arc4random()/0x100000000));
cln r = (((double)arc4random()/0x100000000));
p = 0.0
p = 0
for i = 0 to len(prob) - 1
for j = 1 to _elements
p = p + dblVal( prob[i] )
p += prob(j)
if ( r < p )
if (r < p) then cumulative(j) += 1 : exit for
cnt(i) = cnt(i) + 1
exit for
end if
next
next
next
next
print
printf @" Letter Actual Theoretical"
printf @"------- -------- -----------"
printf @"Item Actual Theoretical"
printf @"---- ------ -----------"
for i = 0 To 7
double temp = cnt(i)/n
for i = 1 to _elements
printf @"%-7s %10.6f %12.6f", item(i), cumulative(i)/samples, prob(i)
sumActual += temp
checksum += cumulative(i)/samples
printf @"%7s %8.6f %10.6f", fn StringUTF8String( items[i] ), temp, fn NumberDoubleValue( prob[i] )
next
next
printf @" -------- ---------"
printf @" -------- -----------"
printf @"%18.6f %6.6f", sumActual, 1.000000
printf @"%17.6f %12.6f", checksum, 1.000000
end fn
end fn


fn ProbabilisticChoice
fn ProbablisticChoice

NSLog( @"%@", fn WindowPrintViewString( 1 ) )


HandleEvents
HandleEvents
Line 1,496: Line 1,492:
{{output}}
{{output}}
<pre>
<pre>
Letter Actual Theoretical
Item Actual Theoretical
------- -------- -----------
---- ------ -----------
aleph 0.200130 0.200000
aleph 0.199966 0.200000
beth 0.167059 0.166667
beth 0.166505 0.166667
gimel 0.142628 0.142857
gimel 0.142958 0.142857
daleth 0.124628 0.125000
daleth 0.124827 0.125000
he 0.111129 0.111111
he 0.111451 0.111111
waw 0.100250 0.100000
waw 0.100095 0.100000
zayin 0.090618 0.090909
zayin 0.090985 0.090909
heth 0.063558 0.063456
heth 0.063213 0.063456
-------- ---------
-------- -----------
1.000000 1.000000
1.000000 1.000000
</pre>
</pre>





=={{header|Go}}==
=={{header|Go}}==