Sleeping Beauty problem: Difference between revisions

Added Oberon-07
(Added Algol 68)
(Added Oberon-07)
Line 690:
<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:
<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}}==
3,049

edits