Sleeping Beauty problem: Difference between revisions
m
syntax highlighting fixup automation
m (→{{header|J}}) |
Thundergnat (talk | contribs) m (syntax highlighting fixup automation) |
||
Line 35:
{{trans|Python}}
<
‘
Run the Sleeping Beauty Problem experiment `repetitions` times, checking to see
Line 61:
V CREDENCE = sleeping_beauty_experiment(1'000'000)
print(‘Results of experiment: Sleeping Beauty should estimate a credence of: ’CREDENCE)</
{{out}}
Line 73:
{{trans|Wren}}
<
wakings: 0
heads: 0
Line 87:
pc: sleepingBeauty 100000
print ["Percentage probability of heads on waking =" pc "%"]</
{{out}}
Line 97:
==={{header|BASIC256}}===
{{trans|FreeBASIC}}
<syntaxhighlight lang="basic256">
iteraciones = 1000000
cara = 0
Line 115:
print "Percentage probability of heads on waking = "; (cara/dormir*100); "%"
end
</syntaxhighlight>
{{out}}
<pre>
Line 122:
==={{header|FreeBASIC}}===
<
Const iteraciones = 1000000
Randomize Timer
Line 136:
Print using "Percentage probability of heads on waking = ###.######%"; (cara/dormir*100)'; "%"
Sleep
</syntaxhighlight>
{{out}}
<pre>
Line 145:
==={{header|GW-BASIC}}===
In this simulation, Sleeping Beauty flips a coin of her own.
<
20 MONDAY = 0 : TUESDAY = 1
30 HEADS = 0 : TAILS = 1
Line 171:
250 IF GUESS = HEADS THEN WHEADS = WHEADS + 1 ELSE CTAILS = CTAILS + 1
260 NEXT DAY
270 RETURN</
{{out}}<pre>
Sleeping Beauty was put through this experiment 300000 times.
Line 183:
==={{header|Yabasic}}===
{{trans|FreeBASIC}}
<syntaxhighlight lang="yabasic">
iteraciones = 1000000
cara = 0
Line 197:
print "Percentage probability of heads on waking = ", (cara/dormir*100), "%"
end
</syntaxhighlight>
{{out}}
<pre>
Line 204:
=={{header|C++}}==
<
#include <random>
Line 229:
std::cout << "Sleeping Beauty should estimate a credence of: "
<< double(heads) / wakenings << '\n';
}</
{{out}}
Line 238:
=={{header|CLU}}==
<
% to use the random number generator.
Line 288:
chance: real := experiment$run(N)
stream$putl(po, f_form(chance, 1, 6))
end start_up </
{{out}}
<pre>Chance of waking up with heads: 0.333758</pre>
Line 295:
{{trans|Swift}}
<
var heads = 0
var wakenings = 0
Line 306:
}
print("Wakenings over \(experiments) experiments: \(wakenings)")
print("Sleeping Beauty should estimate a credence of: \(Float(heads) / Float(wakenings))")</
=={{header|Excel}}==
Line 316:
{{Works with | Office 365 betas 2021}}
<
=LAMBDA(n,
LET(
Line 333:
)
)
)</
{{Out}}
Line 367:
=={{header|F_Sharp|F#}}==
<
// Sleeping Beauty: Nigel Galloway. May 16th., 2021
let heads,woken=let n=System.Random() in {1..1000}|>Seq.fold(fun(h,w) g->match n.Next(2) with 0->(h+1,w+1) |_->(h,w+2))(0,0)
printfn "During 1000 tosses Sleeping Beauty woke %d times, %d times the toss was heads. %.0f%% of times heads had been tossed when she awoke" woken heads (100.0*float(heads)/float(woken))
</syntaxhighlight>
{{out}}
<pre>
Line 378:
=={{header|Factor}}==
{{works with|Factor|0.99 2021-02-05}}
<
: sleeping ( n -- heads wakenings )
Line 385:
"Wakenings over 1,000,000 experiments: " write
1e6 sleeping dup . /f
"Sleeping Beauty should estimate a credence of: " write .</
{{out}}
<pre>
Line 395:
{{trans|Wren}}
{{libheader|Go-rcu}}
<
import (
Line 424:
pc := sleepingBeauty(1e6)
fmt.Printf("Percentage probability of heads on waking = %f%%\n", pc)
}</
{{out}}
Line 434:
=={{header|Haskell}}==
<
import System.Random (randomIO)
import Control.Monad (replicateM)
Line 454:
let (Sum w, Sum h) = foldMap anExperiment tosses
let ratio = fromIntegral h / fromIntegral w
putStrLn $ "Ratio: " ++ show ratio</
<pre>*Main> main
Line 461:
=={{header|J}}==
Simulation code:
<
monday=. ?2
if. -. monday do.
Line 469:
<monday
end.
}}</
Results:<
#sample NB. number of experiments
1000000
Line 483:
0.4996
sample+&#;sample NB. total number of awakenings
2500433</
It's probably worth noting here that the number of heads divided by the number of awakenings would be about 0.3 -- but Sleeping Beauty was not asked to guess whether the coin was heads on Wednesday.
=={{header|Julia}}==
<
Run the Sleeping Beauty Problem experiment `repetitions` times, checking to see
how often we had heads on waking Sleeping Beauty.
Line 523:
CREDENCE = sleeping_beauty_experiment(1_000_000)
println("Results of experiment: Sleeping Beauty should estimate a credence of: ", CREDENCE)
</
Wakenings over 1000000 experiments: 1499534
Results of experiment: Sleeping Beauty should estimate a credence of: 0.33374768428058316
Line 529:
=={{header|Mathematica}}/{{header|Wolfram Language}}==
<
SleepingBeautyExperiment[reps_Integer] := Module[{gotheadsonwaking, wakenings, coinresult},
gotheadsonwaking = 0;
Line 548:
]
out = N@SleepingBeautyExperiment[10^6];
Print["Results of experiment: Sleeping Beauty should estimate a credence of: ", out]</
{{out}}
<pre>Wakenings over 1000000 experiments: 1499714
Line 554:
=={{header|Nim}}==
<
const N = 1_000_000
Line 573:
echo "Wakenings over ", N, " experiments: ", wakenings
echo "Sleeping Beauty should estimate a credence of: ", onHeads / wakenings</
{{out}}
Line 581:
=={{header|Pascal}}==
{{trans|Phix}}
<
program sleepBeau;
uses
Line 603:
end;
writeln(Format(fmt,[iterations,wakings,heads/wakings*100]));
end.</
{{out}}
<pre>Wakings over 1000000 repetitions = 1499741
Line 609:
=={{header|Perl}}==
<
use warnings;
Line 620:
my $trials = 1_000_000;
printf "Wakenings over $trials experiments: %d\nSleeping Beauty should estimate a credence of: %.4f\n", sleeping_beauty($trials);</
{{out}}
<pre>Wakenings over 1000000 experiments: 1499816
Line 626:
=={{header|Phix}}==
<!--<
<span style="color: #008080;">constant</span> <span style="color: #000000;">iterations</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">1_000_000</span><span style="color: #0000FF;">,</span>
<span style="color: #000000;">fmt</span> <span style="color: #0000FF;">=</span> <span style="color: #008000;">"""
Line 639:
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">fmt</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">iterations</span><span style="color: #0000FF;">,</span><span style="color: #000000;">wakings</span><span style="color: #0000FF;">,</span><span style="color: #000000;">heads</span><span style="color: #0000FF;">/</span><span style="color: #000000;">wakings</span><span style="color: #0000FF;">*</span><span style="color: #000000;">100</span><span style="color: #0000FF;">})</span>
<!--</
{{out}}
<small>(You'll get the exact result less than 1% of the time!!)</small>
Line 649:
=={{header|Python}}==
===Procedural===
<
def sleeping_beauty_experiment(repetitions):
Line 683:
CREDENCE = sleeping_beauty_experiment(1_000_000)
print("Results of experiment: Sleeping Beauty should estimate a credence of:", CREDENCE)
</
Wakenings over 1000000 experiments: 1499765
Results of experiment: Sleeping Beauty should estimate a credence of: 0.333542254953276
Line 691:
===Functional===
<
from random import choice
Line 750:
if __name__ == '__main__':
main()
</syntaxhighlight>
{{Out}}
<pre>1500188 wakenings over 1000000 experiments.
Line 759:
=={{header|Quackery}}==
<
[ say "Number of trials: "
Line 778:
10 round vulgar$ echo$ cr ] is trials ( n --> n/d )
1000000 trials</
{{out}}
Line 791:
=={{header|R}}==
There's nothing complicated here. Pretty much every language that resembles C is going to use virtually the same solution.
<
{
wakeCount <- headCount <- 0
Line 801:
headCount/wakeCount
}
print(beautyProblem(10000000))</
{{out}}
<pre>[1] 0.3335838</pre>
Line 807:
=={{header|Raku}}==
<syntaxhighlight lang="raku"
my $gotheadsonwaking = 0;
my $wakenings = 0;
Line 821:
}
say "Results of experiment: Sleeping Beauty should estimate a credence of: ", sleeping-beauty(1_000_000);</
{{out}}
<pre>Wakenings over 1000000 experiments: 1500040
Line 827:
=={{header|Red}}==
<
experiments: 1'000'000
Line 836:
]
print ["Awakenings over" experiments "experiments:" awakenings]
print ["Probability of heads on waking:" heads / awakenings]</
{{out}}
<pre>
Line 845:
=={{header|REXX}}==
When using Regina REXX, the seed specified (for '''random''') was '''46'''.
<
parse arg n seed . /*obtain optional arguments from the CL*/
if n=='' | n=="," then n= 1000000 /*Not specified? Then use the default.*/
Line 859:
exit 0 /*stick a fork in it, we're all done. */
/*──────────────────────────────────────────────────────────────────────────────────────*/
commas: parse arg ?; do jc=length(?)-3 to 1 by -3; ?=insert(',', ?, jc); end; return ?</
{{out|output|text= when using the input of: <tt> , 46 </tt>}}
<pre>
Line 868:
=={{header|Ruby}}==
{{trans|Python}}
<
coin = [:heads, :tails]
gotheadsonwaking = 0
Line 882:
puts "Results of experiment: Sleeping Beauty should estimate
a credence of: #{sleeping_beauty_experiment(1_000_000)}"
</syntaxhighlight>
{{out}}
<pre>Wakenings over 1000000 experiments: 1499604
Line 889:
</pre>
=={{header|Swift}}==
<
var heads = 0
var wakenings = 0
Line 902:
}
print("Wakenings over \(experiments) experiments: \(wakenings)")
print("Sleeping Beauty should estimate a credence of: \(Double(heads) / Double(wakenings))")</
{{out}}
Line 912:
=={{header|Vlang}}==
{{trans|Go}}
<
import rand.seed
Line 935:
pc := sleeping_beauty(1000000)
println("Percentage probability of heads on waking = $pc%")
}</
{{out}}
Line 946:
=={{header|Wren}}==
{{libheader|Wren-fmt}}
<
import "/fmt" for Fmt
Line 968:
var pc = sleepingBeauty.call(1e6)
Fmt.print("Percentage probability of heads on waking = $f\%", pc)</
{{out}}
|