Sleeping Beauty problem: Difference between revisions

Added Easylang
(Added XPL0 example.)
(Added Easylang)
(5 intermediate revisions by 2 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 519 ⟶ 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 979 ⟶ 1,030:
=={{header|Wren}}==
{{libheader|Wren-fmt}}
<syntaxhighlight lang="ecmascriptwren">import "random" for Random
import "./fmt" for Fmt
 
var rand = Random.new()
2,008

edits