Sleeping Beauty problem: Difference between revisions

Adds a Fortran example for the Sleeping Beauty problem
(Adds a Fortran example for the Sleeping Beauty problem)
(6 intermediate revisions by one other user not shown)
Line 68:
Results of experiment: Sleeping Beauty should estimate a credence of: 0.332540916
</pre>
 
=={{header|ALGOL 68}}==
{{Trans|Wren}}
In Algol 68, the generated random numbers are in the semi-open range [0.0..1.0).
<syntaxhighlight lang="algol68">
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
</syntaxhighlight>
{{out}}
<pre>
Wakings over 1000000 repetitions = 1499978
Percentage probability of heads on waking = 33.335289%</pre>
 
=={{header|Arturo}}==
Line 407 ⟶ 436:
</pre>
 
=={{header|Fortran}}==
{{trans|Go}}
Uses random_number from the Fortran 95 standard.
<syntaxhighlight lang="fortran">
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
</syntaxhighlight>
{{out}}
<pre>
wakings over 1000000 reps: 1500326
percentage probability of heads on wake: 0.333044946
</pre>
 
=={{header|FutureBasic}}==
Line 661 ⟶ 742:
<pre>Wakenings over 1000000 experiments: 1499971
Sleeping Beauty should estimate a credence of: 0.3333591116094911</pre>
 
=={{header|Oberon-07}}==
{{Trans|Wren}}
{{works with|Oberonc (Oberon-07 compiler for the JVM)}}
For this task we need random numbers, we first define a DEFINITION module that will allow Oberon-07 code compiled with [[Oberonc_(Oberon-07_compiler_for_the_JVM)|oberonc]] to call methods in a [[Java]] class to obtain random integers:
<syntaxhighlight lang="modula2">
(* Random numbers interface between Oberon-07 and java.util.Random *)
DEFINITION RandomNumbers;
 
(* Returns a random integer in the range 0 .. n - 1 *)
PROCEDURE randomInt( n : INTEGER ) : INTEGER;
 
END RandomNumbers.
</syntaxhighlight>
 
We also need to define the actual code of the RandomNumbers module in Java:
<syntaxhighlight lang="java">
// java class for the Oberon-07 RandomNumbers module
import java.util.Random;
 
public class RandomNumbers
{
 
private static java.util.Random rnd = new java.util.Random();
 
private RandomNumbers(){} // this class can't be instantiated
 
public static int randomInt( int n )
{
return rnd.nextInt( n );
} // randomInt
 
} // RandomNumbers
</syntaxhighlight>
 
After compiling the above Oberon-07 and Java sources, we can now use them in the SleepingBeauty module to solve the Task:
 
<syntaxhighlight lang="modula2">
(* sleeping beauty problem - translated from the Wren sample *)
MODULE SleepingBeauty;
IMPORT
Out, RandomNumbers;
 
VAR pc : REAL;
 
PROCEDURE sleepingBeauty( reps : INTEGER ) : REAL;
VAR wakings, heads, i : INTEGER;
BEGIN
wakings := 0; heads := 0;
FOR i := 1 TO reps DO
wakings := wakings + 1;
IF RandomNumbers.randomInt( 2 ) = 1 THEN (* 1 = heads, 0 = tails say *)
heads := heads + 1
ELSE
wakings := wakings + 1
END
END;
Out.String( "Wakings over " );Out.Int( reps, 0 );
Out.String( " repetitions = " );Out.Int( wakings, 0 );Out.Ln
RETURN ( FLT( heads ) / FLT( wakings ) ) * 100.0
END sleepingBeauty;
BEGIN
pc := sleepingBeauty( 1000000 );
Out.String( "Percentage probability of heads on waking = " );Out.Real( pc, 10 );Out.String( "%" );Out.Ln
END SleepingBeauty.
</syntaxhighlight>
 
{{out}}
<pre>
Wakings over 1000000 repetitions = 1500298
Percentage probability of heads on waking = 33.306850%
</pre>
 
=={{header|Pascal}}==
1

edit