Sleeping Beauty problem: Difference between revisions

J
No edit summary
(J)
Line 458:
<pre>*Main> main
Ratio: 0.33339378051805013</pre>
 
=={{header|J}}==
Simulation code:
<lang J>sb=: {{
monday=. ?2
if. -. monday do.
tuesday=. ?2
<monday,tuesday
else.
<monday
end.
}}</lang>
 
Results:<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
(+/%#);sample NB. odds of heads at time of question
0.4996
sample+&#;sample NB. total number of awakenings
2500433</lang>
 
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}}==
6,951

edits