Sleeping Beauty problem: Difference between revisions

Added Easylang
m (syntax highlighting fixup automation)
(Added Easylang)
(8 intermediate revisions by 5 users not shown)
Line 307:
print("Wakenings over \(experiments) experiments: \(wakenings)")
print("Sleeping Beauty should estimate a credence of: \(Float(heads) / Float(wakenings))")</syntaxhighlight>
 
=={{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 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}}==
Line 486 ⟶ 534:
 
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}}==
Line 910 ⟶ 994:
</pre>
 
=={{header|V (Vlang)}}==
{{trans|Go}}
<syntaxhighlight lang="v (vlang)">import rand
import rand.seed
 
Line 946 ⟶ 1,030:
=={{header|Wren}}==
{{libheader|Wren-fmt}}
<syntaxhighlight lang="ecmascriptwren">import "random" for Random
import "./fmt" for Fmt
 
var rand = Random.new()
Line 975 ⟶ 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>
1,980

edits