Sleeping Beauty problem: Difference between revisions

m
syntax highlighting fixup automation
m (syntax highlighting fixup automation)
Line 35:
{{trans|Python}}
 
<langsyntaxhighlight lang="11l">F sleeping_beauty_experiment(repetitions)
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)</langsyntaxhighlight>
 
{{out}}
Line 73:
{{trans|Wren}}
 
<langsyntaxhighlight lang="rebol">sleepingBeauty: function [reps][
wakings: 0
heads: 0
Line 87:
pc: sleepingBeauty 100000
print ["Percentage probability of heads on waking =" pc "%"]</langsyntaxhighlight>
 
{{out}}
Line 97:
==={{header|BASIC256}}===
{{trans|FreeBASIC}}
<syntaxhighlight lang="basic256">
<lang BASIC256>
iteraciones = 1000000
cara = 0
Line 115:
print "Percentage probability of heads on waking = "; (cara/dormir*100); "%"
end
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 122:
 
==={{header|FreeBASIC}}===
<langsyntaxhighlight lang="freebasic">
Const iteraciones = 1000000
Randomize Timer
Line 136:
Print using "Percentage probability of heads on waking = ###.######%"; (cara/dormir*100)'; "%"
Sleep
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 145:
==={{header|GW-BASIC}}===
In this simulation, Sleeping Beauty flips a coin of her own.
<langsyntaxhighlight lang="gwbasic">10 RANDOMIZE TIMER
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</langsyntaxhighlight>
{{out}}<pre>
Sleeping Beauty was put through this experiment 300000 times.
Line 183:
==={{header|Yabasic}}===
{{trans|FreeBASIC}}
<syntaxhighlight lang="yabasic">
<lang Yabasic>
iteraciones = 1000000
cara = 0
Line 197:
print "Percentage probability of heads on waking = ", (cara/dormir*100), "%"
end
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 204:
 
=={{header|C++}}==
<langsyntaxhighlight lang="cpp">#include <iostream>
#include <random>
 
Line 229:
std::cout << "Sleeping Beauty should estimate a credence of: "
<< double(heads) / wakenings << '\n';
}</langsyntaxhighlight>
 
{{out}}
Line 238:
 
=={{header|CLU}}==
<langsyntaxhighlight lang="clu">% This program needs to be merged with PCLU's "misc" library
% to use the random number generator.
 
Line 288:
chance: real := experiment$run(N)
stream$putl(po, f_form(chance, 1, 6))
end start_up </langsyntaxhighlight>
{{out}}
<pre>Chance of waking up with heads: 0.333758</pre>
Line 295:
{{trans|Swift}}
 
<langsyntaxhighlight lang="dyalect">let experiments = 10000
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))")</langsyntaxhighlight>
 
=={{header|Excel}}==
Line 316:
 
{{Works with | Office 365 betas 2021}}
<langsyntaxhighlight lang="lisp">SLEEPINGB
=LAMBDA(n,
LET(
Line 333:
)
)
)</langsyntaxhighlight>
{{Out}}
 
Line 367:
 
=={{header|F_Sharp|F#}}==
<langsyntaxhighlight lang="fsharp">
// 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>
</lang>
{{out}}
<pre>
Line 378:
=={{header|Factor}}==
{{works with|Factor|0.99 2021-02-05}}
<langsyntaxhighlight lang="factor">USING: combinators.random io kernel math prettyprint ;
 
: 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 .</langsyntaxhighlight>
{{out}}
<pre>
Line 395:
{{trans|Wren}}
{{libheader|Go-rcu}}
<langsyntaxhighlight lang="go">package main
 
import (
Line 424:
pc := sleepingBeauty(1e6)
fmt.Printf("Percentage probability of heads on waking = %f%%\n", pc)
}</langsyntaxhighlight>
 
{{out}}
Line 434:
 
=={{header|Haskell}}==
<langsyntaxhighlight lang="haskell">import Data.Monoid (Sum(..))
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</langsyntaxhighlight>
 
<pre>*Main> main
Line 461:
=={{header|J}}==
Simulation code:
<langsyntaxhighlight Jlang="j">sb=: {{
monday=. ?2
if. -. monday do.
Line 469:
<monday
end.
}}</langsyntaxhighlight>
 
Results:<langsyntaxhighlight Jlang="j"> sample=: sb"0 i.1e6 NB. simulate a million mondays
#sample NB. number of experiments
1000000
Line 483:
0.4996
sample+&#;sample NB. total number of awakenings
2500433</langsyntaxhighlight>
 
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}}==
<langsyntaxhighlight lang="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)
</langsyntaxhighlight>{{out}}<pre>
Wakenings over 1000000 experiments: 1499534
Results of experiment: Sleeping Beauty should estimate a credence of: 0.33374768428058316
Line 529:
 
=={{header|Mathematica}}/{{header|Wolfram Language}}==
<langsyntaxhighlight Mathematicalang="mathematica">ClearAll[SleepingBeautyExperiment]
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]</langsyntaxhighlight>
{{out}}
<pre>Wakenings over 1000000 experiments: 1499714
Line 554:
 
=={{header|Nim}}==
<langsyntaxhighlight Nimlang="nim">import random
 
const N = 1_000_000
Line 573:
 
echo "Wakenings over ", N, " experiments: ", wakenings
echo "Sleeping Beauty should estimate a credence of: ", onHeads / wakenings</langsyntaxhighlight>
 
{{out}}
Line 581:
=={{header|Pascal}}==
{{trans|Phix}}
<langsyntaxhighlight lang="pascal">
program sleepBeau;
uses
Line 603:
end;
writeln(Format(fmt,[iterations,wakings,heads/wakings*100]));
end.</langsyntaxhighlight>
{{out}}
<pre>Wakings over 1000000 repetitions = 1499741
Line 609:
 
=={{header|Perl}}==
<langsyntaxhighlight lang="perl">use strict;
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);</langsyntaxhighlight>
{{out}}
<pre>Wakenings over 1000000 experiments: 1499816
Line 626:
 
=={{header|Phix}}==
<!--<langsyntaxhighlight Phixlang="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: #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>
<!--</langsyntaxhighlight>-->
{{out}}
<small>(You'll get the exact result less than 1% of the time!!)</small>
Line 649:
=={{header|Python}}==
===Procedural===
<langsyntaxhighlight lang="python">from random import choice
 
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)
</langsyntaxhighlight>{{out}}<pre>
Wakenings over 1000000 experiments: 1499765
Results of experiment: Sleeping Beauty should estimate a credence of: 0.333542254953276
Line 691:
===Functional===
 
<langsyntaxhighlight lang="python">'''Sleeping Beauty Problem'''
 
from random import choice
Line 750:
if __name__ == '__main__':
main()
</syntaxhighlight>
</lang>
{{Out}}
<pre>1500188 wakenings over 1000000 experiments.
Line 759:
=={{header|Quackery}}==
 
<langsyntaxhighlight Quackerylang="quackery"> [ $ "bigrat.qky" loadfile ] now!
 
[ say "Number of trials: "
Line 778:
10 round vulgar$ echo$ cr ] is trials ( n --> n/d )
 
1000000 trials</langsyntaxhighlight>
 
{{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.
<langsyntaxhighlight lang="rsplus">beautyProblem <- function(n)
{
wakeCount <- headCount <- 0
Line 801:
headCount/wakeCount
}
print(beautyProblem(10000000))</langsyntaxhighlight>
{{out}}
<pre>[1] 0.3335838</pre>
Line 807:
=={{header|Raku}}==
 
<syntaxhighlight lang="raku" perl6line>sub sleeping-beauty ($trials) {
my $gotheadsonwaking = 0;
my $wakenings = 0;
Line 821:
}
 
say "Results of experiment: Sleeping Beauty should estimate a credence of: ", sleeping-beauty(1_000_000);</langsyntaxhighlight>
{{out}}
<pre>Wakenings over 1000000 experiments: 1500040
Line 827:
 
=={{header|Red}}==
<langsyntaxhighlight lang="rebol">Red ["Sleeping Beauty problem"]
 
experiments: 1'000'000
Line 836:
]
print ["Awakenings over" experiments "experiments:" awakenings]
print ["Probability of heads on waking:" heads / awakenings]</langsyntaxhighlight>
{{out}}
<pre>
Line 845:
=={{header|REXX}}==
When using Regina REXX, &nbsp; the seed specified &nbsp; (for '''random''') &nbsp; was &nbsp; '''46'''.
<langsyntaxhighlight 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*/
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 ?</langsyntaxhighlight>
{{out|output|text=&nbsp; when using the input of: &nbsp; &nbsp; <tt> , &nbsp; 46 </tt>}}
<pre>
Line 868:
=={{header|Ruby}}==
{{trans|Python}}
<langsyntaxhighlight lang="ruby">def sleeping_beauty_experiment(n)
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>
</lang>
{{out}}
<pre>Wakenings over 1000000 experiments: 1499604
Line 889:
</pre>
=={{header|Swift}}==
<langsyntaxhighlight lang="swift">let experiments = 1000000
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))")</langsyntaxhighlight>
 
{{out}}
Line 912:
=={{header|Vlang}}==
{{trans|Go}}
<langsyntaxhighlight lang="vlang">import rand
import rand.seed
 
Line 935:
pc := sleeping_beauty(1000000)
println("Percentage probability of heads on waking = $pc%")
}</langsyntaxhighlight>
 
{{out}}
Line 946:
=={{header|Wren}}==
{{libheader|Wren-fmt}}
<langsyntaxhighlight lang="ecmascript">import "random" for Random
import "/fmt" for Fmt
 
Line 968:
 
var pc = sleepingBeauty.call(1e6)
Fmt.print("Percentage probability of heads on waking = $f\%", pc)</langsyntaxhighlight>
 
{{out}}
10,333

edits