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"> |
||
_elements = 8 |
|||
local fn |
local fn ProbabilisticChoice |
||
double prob(_elements), cumulative(_elements) |
|||
CFArrayRef items = @[@"aleph",@"beth",@"gimel",@"daleth",@"he",@"waw",@"zayin",@"heth"] |
|||
Str15 item(_elements) |
|||
CFArrayRef prob = @[¬ |
|||
⚫ | |||
fn NumberWithDouble(1/5.0),¬ |
|||
⚫ | |||
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" |
|||
⚫ | |||
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 |
|||
sum = sum + fn NumberDoubleValue( prob[i] ) |
|||
⚫ | |||
sum += prob(i) |
|||
next |
next |
||
if ( |
if abs(sum-1) > samples then print "Probabilities don't sum to 1." : exit fn |
||
for |
for i = 1 to samples |
||
cln r = (((double)arc4random()/0x100000000)); |
cln r = (((double)arc4random()/0x100000000)); |
||
p = |
p = 0 |
||
for |
for j = 1 to _elements |
||
p = |
p += prob(j) |
||
if ( |
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 = 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 @"% |
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> |
||
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> |
</pre> |
||
=={{header|Go}}== |
=={{header|Go}}== |