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