Probabilistic choice: Difference between revisions

Major rewrite to simplify code
No edit summary
(Major rewrite to simplify code)
Line 1,445:
=={{header|FutureBasic}}==
<syntaxhighlight lang="futurebasic">
_arrItems_elements = 78
 
local fn ProbablisticChoiceProbabilisticChoice
double prob(_elements), cumulative(_elements)
CFArrayRef items = @[@"aleph",@"beth",@"gimel",@"daleth",@"he",@"waw",@"zayin",@"heth"]
Str15 item(_elements)
CFArrayRef prob = @[¬
double pr, rp, sum = 0, cnt(_arrItems), sumActualchecksum = 0
fn NumberWithDouble(1/5.0),¬
long triali, ij, nsamples = 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
forprob(1) i= 1/5.0 : prob(2) = 1/6.0 to len: 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 = 01 Toto 78
sum += prob(i)
next
if abs( sum-1) !=> 1 )samples then print "Probabilities don't sum to 1." : exit fn
for triali = 1 to nsamples
cln r = (((double)arc4random()/0x100000000));
p = 0.0
for ij = 01 to len(prob) - 1_elements
p += p + dblValprob( prob[i] j)
if ( r < p) then cumulative(j) += 1 : exit for
cnt(i) = cnt(i) + 1
exit for
end if
next
next
print
printf @" Letter Actual Theoretical"
printf @"-------Item -------- -----------Actual Theoretical"
printf @"---- ------ -----------"
for i = 0 To 7
for i double= temp1 =to cnt(i)/n_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
printf @" -------- -----------"
printf @"%1817.6f %612.6f", sumActualchecksum, 1.000000
end fn
 
fn ProbabilisticChoice
fn ProbablisticChoice
 
NSLog( @"%@", fn WindowPrintViewString( 1 ) )
 
HandleEvents
Line 1,496 ⟶ 1,492:
{{output}}
<pre>
LetterItem Actual Theoretical
------- -------- -----------
aleph aleph 0.200130199966 0.200000
beth beth 0.167059166505 0.166667
gimel gimel 0.142628142958 0.142857
daleth 0.124628124827 0.125000
he he 0.111129111451 0.111111
waw waw 0.100250100095 0.100000
zayin zayin 0.090618090985 0.090909
heth heth 0.063558063213 0.063456
-------- -----------
1.000000 1.000000
</pre>
 
 
 
 
=={{header|Go}}==
719

edits