Sleeping Beauty problem: Difference between revisions

(Adds a Fortran example for the Sleeping Beauty problem)
 
Line 649:
Awakenings over 1000000 experiments: 1499522
Sleeping Beauty should estimate a credence of: 0.334
</pre>
 
=={{header|jq}}==
'''Adapted from [[#Wren|Wren]]'''
 
'''Works with jq, the C implementation of jq'''
 
'''Works with gojq, the Go implementation of jq'''
 
'''Works with jaq, the Rust implementation of jq'''
 
Since jq does not currently have a built-in PRNG,
/dev/random will be used as a source of entropy. An invocation
of jq along the lines of the following would be appropriate
in a typical command-line environment:
<pre>
< /dev/random tr -cd '0-9' | fold -w 1 | jq -cnr -f sleeping.jq
</pre>
where "sleeping.jq" is the name of a file containing the following jq program.
<syntaxhighlight lang="jq">
# Output: a PRN in range(0; .)
def prn:
if . == 1 then 0
else . as $n
| (($n-1)|tostring|length) as $w
| [limit($w; inputs)] | join("") | tonumber
| if . < $n then . else ($n | prn) end
end;
 
def round($ndec): pow(10;$ndec) as $p | . * $p | round / $p;
# Output: {n, heads, wakings}
def sleepingBeauty:
{ n: ., wakings: 0, heads: 0 }
| reduce range(0; .n) as $i (.;
(2|prn) as $coin # heads = 0, tails = 1 say
| .wakings += 1
| if $coin == 0 then .heads += 1
else .wakings += 1
end );
 
1000000
| sleepingBeauty
| "Wakings over \(.n) repetitions = \(.wakings).",
"Percentage probability of heads on waking = \(100*.heads/.wakings | round(3))%"
</syntaxhighlight>
{{output}}
<pre>
Wakings over 1000000 repetitions = 1499524.
Percentage probability of heads on waking = 33.375%
</pre>
 
2,502

edits