Sleeping Beauty problem: Difference between revisions
Added Easylang
No edit summary |
(Added Easylang) |
||
(11 intermediate revisions by 7 users not shown) | |||
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|EasyLang}}==
<syntaxhighlight>
reps = 1e6
for i to reps
coin = randint 2
wakings += 1
if coin = 1
heads += 1
else
wakings += 1
.
.
print "Chance of waking up with heads: " & heads / wakings * 100 & "%"
</syntaxhighlight>
=={{header|Excel}}==
Line 316 ⟶ 331:
{{Works with | Office 365 betas 2021}}
<
=LAMBDA(n,
LET(
Line 333 ⟶ 348:
)
)
)</
{{Out}}
Line 367 ⟶ 382:
=={{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 ⟶ 393:
=={{header|Factor}}==
{{works with|Factor|0.99 2021-02-05}}
<
: sleeping ( n -- heads wakenings )
Line 385 ⟶ 400:
"Wakenings over 1,000,000 experiments: " write
1e6 sleeping dup . /f
"Sleeping Beauty should estimate a credence of: " write .</
{{out}}
<pre>
Line 391 ⟶ 406:
Sleeping Beauty should estimate a credence of: 0.3332204540015612
</pre>
=={{header|FutureBasic}}==
<syntaxhighlight lang="futurebasic">
_iterations = 1000000
local fn SleepingBeauty
NSUInteger i
CGFloat heads = 0, sleep = 0
for i = 1 to _iterations
NSInteger coinToss = int( rnd(2) )
sleep++
if coinToss = 1 then heads++ else sleep++
next
printf @"Awakenings over %lld sleep cycles = %.f", _iterations, sleep
printf @"Percent probability of heads on waking = %.4f%%", heads / sleep * 100
end fn
randomize
fn SleepingBeauty
HandleEvents
</syntaxhighlight>
{{output}}
<pre>
Awakenings over 1000000 sleep cycles = 1499725
Percent probability of heads on waking = 33.3578%
</pre>
=={{header|Go}}==
{{trans|Wren}}
{{libheader|Go-rcu}}
<
import (
Line 424 ⟶ 472:
pc := sleepingBeauty(1e6)
fmt.Printf("Percentage probability of heads on waking = %f%%\n", pc)
}</
{{out}}
Line 434 ⟶ 482:
=={{header|Haskell}}==
<
import System.Random (randomIO)
import Control.Monad (replicateM)
Line 454 ⟶ 502:
let (Sum w, Sum h) = foldMap anExperiment tosses
let ratio = fromIntegral h / fromIntegral w
putStrLn $ "Ratio: " ++ show ratio</
<pre>*Main> main
Ratio: 0.33339378051805013</pre>
=={{header|J}}==
Simulation code:
<syntaxhighlight lang="j">sb=: {{
monday=. ?2
if. -. monday do.
tuesday=. ?2
<monday,tuesday
else.
<monday
end.
}}</syntaxhighlight>
Results:<syntaxhighlight lang="j"> sample=: sb"0 i.1e6 NB. simulate a million mondays
#sample NB. number of experiments
1000000
#;sample NB. number of questions
1500433
+/;sample NB. number of heads
749617
+/0={.@>sample NB. how many times was sleeping beauty drugged?
500433
(+/%#);sample NB. odds of heads at time of question
0.4996
sample+&#;sample NB. total number of awakenings
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.
=={{header|Java}}==
<syntaxhighlight lang="java">
import java.util.concurrent.ThreadLocalRandom;
public final class SleepingBeauty {
public static void main(String[] aArgs) {
final int experiments = 1_000_000;
ThreadLocalRandom random = ThreadLocalRandom.current();
enum Coin { HEADS, TAILS }
int heads = 0;
int awakenings = 0;
for ( int i = 0; i < experiments; i++ ) {
Coin coin = Coin.values()[random.nextInt(0, 2)];
switch ( coin ) {
case HEADS -> { awakenings += 1; heads += 1; }
case TAILS -> awakenings += 2;
}
}
System.out.println("Awakenings over " + experiments + " experiments: " + awakenings);
String credence = String.format("%.3f", (double) heads / awakenings);
System.out.println("Sleeping Beauty should estimate a credence of: " + credence);
}
}
</syntaxhighlight>
{{ out }}
<pre>
Awakenings over 1000000 experiments: 1499522
Sleeping Beauty should estimate a credence of: 0.334
</pre>
=={{header|Julia}}==
<
Run the Sleeping Beauty Problem experiment `repetitions` times, checking to see
how often we had heads on waking Sleeping Beauty.
Line 495 ⟶ 607:
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 501 ⟶ 613:
=={{header|Mathematica}}/{{header|Wolfram Language}}==
<
SleepingBeautyExperiment[reps_Integer] := Module[{gotheadsonwaking, wakenings, coinresult},
gotheadsonwaking = 0;
Line 520 ⟶ 632:
]
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 526 ⟶ 638:
=={{header|Nim}}==
<
const N = 1_000_000
Line 545 ⟶ 657:
echo "Wakenings over ", N, " experiments: ", wakenings
echo "Sleeping Beauty should estimate a credence of: ", onHeads / wakenings</
{{out}}
Line 553 ⟶ 665:
=={{header|Pascal}}==
{{trans|Phix}}
<
program sleepBeau;
uses
Line 575 ⟶ 687:
end;
writeln(Format(fmt,[iterations,wakings,heads/wakings*100]));
end.</
{{out}}
<pre>Wakings over 1000000 repetitions = 1499741
Line 581 ⟶ 693:
=={{header|Perl}}==
<
use warnings;
Line 592 ⟶ 704:
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 598 ⟶ 710:
=={{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 611 ⟶ 723:
<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 621 ⟶ 733:
=={{header|Python}}==
===Procedural===
<
def sleeping_beauty_experiment(repetitions):
Line 655 ⟶ 767:
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 663 ⟶ 775:
===Functional===
<
from random import choice
Line 722 ⟶ 834:
if __name__ == '__main__':
main()
</syntaxhighlight>
{{Out}}
<pre>1500188 wakenings over 1000000 experiments.
Line 731 ⟶ 843:
=={{header|Quackery}}==
<
[ say "Number of trials: "
Line 750 ⟶ 862:
10 round vulgar$ echo$ cr ] is trials ( n --> n/d )
1000000 trials</
{{out}}
Line 763 ⟶ 875:
=={{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 773 ⟶ 885:
headCount/wakeCount
}
print(beautyProblem(10000000))</
{{out}}
<pre>[1] 0.3335838</pre>
Line 779 ⟶ 891:
=={{header|Raku}}==
<syntaxhighlight lang="raku"
my $gotheadsonwaking = 0;
my $wakenings = 0;
Line 793 ⟶ 905:
}
say "Results of experiment: Sleeping Beauty should estimate a credence of: ", sleeping-beauty(1_000_000);</
{{out}}
<pre>Wakenings over 1000000 experiments: 1500040
Line 799 ⟶ 911:
=={{header|Red}}==
<
experiments: 1'000'000
Line 808 ⟶ 920:
]
print ["Awakenings over" experiments "experiments:" awakenings]
print ["Probability of heads on waking:" heads / awakenings]</
{{out}}
<pre>
Line 817 ⟶ 929:
=={{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 831 ⟶ 943:
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 840 ⟶ 952:
=={{header|Ruby}}==
{{trans|Python}}
<
coin = [:heads, :tails]
gotheadsonwaking = 0
Line 854 ⟶ 966:
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 861 ⟶ 973:
</pre>
=={{header|Swift}}==
<
var heads = 0
var wakenings = 0
Line 874 ⟶ 986:
}
print("Wakenings over \(experiments) experiments: \(wakenings)")
print("Sleeping Beauty should estimate a credence of: \(Double(heads) / Double(wakenings))")</
{{out}}
Line 882 ⟶ 994:
</pre>
=={{header|V (Vlang)}}==
{{trans|Go}}
<syntaxhighlight lang="v (vlang)">import rand
import rand.seed
Line 907 ⟶ 1,019:
pc := sleeping_beauty(1000000)
println("Percentage probability of heads on waking = $pc%")
}</
{{out}}
Line 918 ⟶ 1,030:
=={{header|Wren}}==
{{libheader|Wren-fmt}}
<
import "./fmt" for Fmt
var rand = Random.new()
Line 940 ⟶ 1,052:
var pc = sleepingBeauty.call(1e6)
Fmt.print("Percentage probability of heads on waking = $f\%", pc)</
{{out}}
Line 947 ⟶ 1,059:
Wakings over 1,000,000 repetitions = 1,500,321
Percentage probability of heads on waking = 33.304806%
</pre>
=={{header|XPL0}}==
{{trans|Wren}}
<syntaxhighlight lang "XPL0">include xpllib; \for Print
func real SleepingBeauty(Reps);
int Reps, Wakings, Heads, Coin, I;
[Wakings:= 0; Heads:= 0;
for I:= 0 to Reps-1 do
[Coin:= Ran(2); \heads = 0, tails = 1 say
Wakings:= Wakings + 1;
if Coin = 0 then Heads:= Heads + 1
else Wakings:= Wakings + 1;
];
Print("Wakings over %d repetitions = %d\n", Reps, Wakings);
return float(Heads) / float(Wakings) * 100.;
];
real PC;
[PC:= SleepingBeauty(1_000_000);
Print("Percentage probability of heads on waking = %1.6f\%\n", PC);
]</syntaxhighlight>
{{out}}
<pre>
Wakings over 1000000 repetitions = 1500013
Percentage probability of heads on waking = 33.332178%
</pre>
|