Data update
This commit is contained in:
parent
29a5eea0d4
commit
5c1bb7bfa9
2011 changed files with 35081 additions and 3229 deletions
|
|
@ -0,0 +1,19 @@
|
|||
BEGIN # sleeping beauty problem - translated from the Wren sample #
|
||||
PROC sleeping beauty = ( INT reps )REAL:
|
||||
BEGIN
|
||||
INT wakings := 0, heads := 0;
|
||||
FOR i TO reps DO
|
||||
wakings +:= 1;
|
||||
IF next random < 0.5 THEN # [0..0.5) = heads, [0.5..1.0) = tails say #
|
||||
heads +:= 1
|
||||
ELSE
|
||||
wakings +:= 1
|
||||
FI
|
||||
OD;
|
||||
print( ( "Wakings over ", whole( reps, 0 ), " repetitions = ", whole( wakings, 0 ), newline ) );
|
||||
( heads / wakings ) * 100
|
||||
END; # sleeping beauty #
|
||||
|
||||
REAL pc = sleeping beauty( 1 000 000 );
|
||||
print( ( "Percentage probability of heads on waking = ", fixed( pc, -10, 6 ), "%", newline ) )
|
||||
END
|
||||
|
|
@ -0,0 +1,43 @@
|
|||
program sleepingbeauty
|
||||
implicit none
|
||||
|
||||
integer :: total_reps
|
||||
integer :: result_wakings
|
||||
real :: result_percent
|
||||
|
||||
total_reps = 1e6
|
||||
|
||||
call sleepingOp(total_reps, result_wakings, result_percent)
|
||||
|
||||
print *, "wakings over", total_reps, "reps: ", result_wakings
|
||||
print *, "percentage probability of heads on wake:", result_percent
|
||||
|
||||
contains
|
||||
|
||||
subroutine sleepingOp(reps, wakings, percent)
|
||||
integer, intent(in) :: reps
|
||||
integer, intent(out) :: wakings
|
||||
real, intent(out) :: percent
|
||||
|
||||
integer :: heads
|
||||
integer :: i
|
||||
real :: coin
|
||||
|
||||
wakings = 0
|
||||
heads = 0
|
||||
|
||||
do i = 0, reps, 1
|
||||
call random_number(coin)
|
||||
wakings = wakings + 1
|
||||
if (coin > 0.5) then
|
||||
heads = heads + 1
|
||||
else
|
||||
wakings = wakings + 1
|
||||
end if
|
||||
end do
|
||||
|
||||
percent = real(heads) / real(wakings)
|
||||
|
||||
end subroutine sleepingOp
|
||||
|
||||
end program sleepingbeauty
|
||||
|
|
@ -1,17 +1,17 @@
|
|||
_iterations = 1000000
|
||||
|
||||
local fn SleepingBeauty
|
||||
NSUInteger i
|
||||
CGFloat heads = 0, sleep = 0
|
||||
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
|
||||
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
|
||||
printf @"Awakenings over %lld sleep cycles = %.f", _iterations, sleep
|
||||
printf @"Percent probability of heads on waking = %.4f%%", heads / sleep * 100
|
||||
end fn
|
||||
|
||||
randomize
|
||||
|
|
|
|||
25
Task/Sleeping-Beauty-problem/Jq/sleeping-beauty-problem.jq
Normal file
25
Task/Sleeping-Beauty-problem/Jq/sleeping-beauty-problem.jq
Normal file
|
|
@ -0,0 +1,25 @@
|
|||
# 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))%"
|
||||
Loading…
Add table
Add a link
Reference in a new issue