Data update
This commit is contained in:
parent
81fd053722
commit
52a6ef48dd
10248 changed files with 63654 additions and 6775 deletions
32
Task/Sleeping-Beauty-problem/Ada/sleeping-beauty-problem.ada
Normal file
32
Task/Sleeping-Beauty-problem/Ada/sleeping-beauty-problem.ada
Normal file
|
|
@ -0,0 +1,32 @@
|
|||
with Ada.Numerics.Discrete_Random;
|
||||
with Ada.Text_IO; use Ada.Text_IO;
|
||||
|
||||
procedure Sleeping_Beauty is
|
||||
type Coin is (Heads, Tails);
|
||||
package Random_Coin is new Ada.Numerics.Discrete_Random (Coin); use Random_Coin;
|
||||
package FIO is new Float_IO (Float);
|
||||
Tosser : Generator;
|
||||
Probability : Float;
|
||||
|
||||
function Experiment (Tosses : Integer) return Float is
|
||||
Awakenings, Heads_Count : Integer := 0;
|
||||
begin
|
||||
for Iteration in 1 .. Tosses loop
|
||||
case Random (Tosser) is
|
||||
when Heads =>
|
||||
Awakenings := Awakenings + 1;
|
||||
Heads_Count := Heads_Count + 1;
|
||||
when Tails =>
|
||||
Awakenings := Awakenings + 2;
|
||||
end case;
|
||||
end loop;
|
||||
Put_Line ("Awakenings over" & Tosses'Image & " iterations:" & Awakenings'Image);
|
||||
return Float (Heads_Count) / Float (Awakenings) * 100.0;
|
||||
end Experiment;
|
||||
|
||||
begin
|
||||
Reset (Tosser);
|
||||
Probability := Experiment (1_000_000);
|
||||
Put ("Percentage probability of heads on waking:");
|
||||
FIO.Put (Probability, 3, 5, 0);
|
||||
end Sleeping_Beauty;
|
||||
|
|
@ -1,6 +1,6 @@
|
|||
reps = 1e6
|
||||
for i to reps
|
||||
coin = randint 2
|
||||
coin = random 2
|
||||
wakings += 1
|
||||
if coin = 1
|
||||
heads += 1
|
||||
|
|
|
|||
|
|
@ -0,0 +1,7 @@
|
|||
(* 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.
|
||||
|
|
@ -0,0 +1,16 @@
|
|||
// 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
|
||||
|
|
@ -0,0 +1,27 @@
|
|||
(* 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
|
||||
INC(wakings);
|
||||
IF RandomNumbers.randomInt( 2 ) = 1 THEN (* 1 = heads, 0 = tails say *)
|
||||
INC(heads)
|
||||
ELSE
|
||||
INC(wakings)
|
||||
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.
|
||||
Loading…
Add table
Add a link
Reference in a new issue