package Ada.Numerics.Float_Random is
-- Basic facilities
type Generator is limited private;
subtype Uniformly_Distributed is Float range 0.0 .. 1.0; function Random (Gen : Generator) return Uniformly_Distributed;
procedure Reset (Gen : in Generator; Initiator : in Integer); procedure Reset (Gen : in Generator);
-- Advanced facilities
type State is private;
procedure Save (Gen : in Generator; To_State : out State); procedure Reset (Gen : in Generator; From_State : in State);
Max_Image_Width : constant := implementation-defined integer value;
function Image (Of_State : State) return String; function Value (Coded_State : String) return State;
private ... -- not specified by the language end Ada.Numerics.Float_Random;
generic type Result_Subtype is (<>); package Ada.Numerics.Discrete_Random is
-- Basic facilities
type Generator is limited private;
function Random (Gen : Generator) return Result_Subtype;
procedure Reset (Gen : in Generator; Initiator : in Integer); procedure Reset (Gen : in Generator);
-- Advanced facilities
type State is private;
procedure Save (Gen : in Generator; To_State : out State); procedure Reset (Gen : in Generator; From_State : in State);
Max_Image_Width : constant := implementation-defined integer value;
function Image (Of_State : State) return String; function Value (Coded_State : String) return State;
private ... -- not specified by the language end Ada.Numerics.Discrete_Random;
function Random (Gen : Generator) return Uniformly_Distributed; function Random (Gen : Generator) return Result_Subtype;
procedure Reset (Gen : in Generator; Initiator : in Integer); procedure Reset (Gen : in Generator);
procedure Save (Gen : in Generator; To_State : out State); procedure Reset (Gen : in Generator; From_State : in State);
function Image (Of_State : State) return String; function Value (Coded_State : String) return State;
NOTES
Integer(Float(M) * Random(G)) mod M
-Log(Random(G) + Float'Model_Small))
with Ada.Numerics.Discrete_Random; procedure Dice_Game is subtype Die is Integer range 1 .. 6; subtype Dice is Integer range 2*Die'First .. 2*Die'Last; package Random_Die is new Ada.Numerics.Discrete_Random (Die); use Random_Die; G : Generator; D : Dice; begin Reset (G); -- Start the generator in a unique state in each run loop -- Roll a pair of dice; sum and process the results D := Random(G) + Random(G); ... end loop; end Dice_Game;
with Ada.Numerics.Discrete_Random; procedure Flip_A_Coin is type Coin is (Heads, Tails); package Random_Coin is new Ada.Numerics.Discrete_Random (Coin); use Random_Coin; G : Generator; begin Reset (G); -- Start the generator in a unique state in each run loop -- Toss a coin and process the result case Random(G) is when Heads => ... when Tails => ... end case; ... end loop; end Flip_A_Coin;
with Ada.Numerics.Float_Random; procedure Parallel_Simulation is use Ada.Numerics.Float_Random; task type Worker is entry Initialize_Generator (Initiator : in Integer); ... end Worker; W : array (1 .. 10) of Worker; task body Worker is G : Generator; Probability_Of_Event : Uniformly_Distributed; begin accept Initialize_Generator (Initiator : in Integer) do Reset (G, Initiator); end Initialize_Generator; loop ... Probability_Of_Event := Random(G); ... end loop; end Worker; begin -- Initialize the generators in the Worker tasks -- to different states for I in W'Range loop W(I).Initialize_Generator (I); end loop; ... -- Wait for the Worker tasks to terminate end Parallel_Simulation;
NOTES
Go to the first, previous, next, last section, table of contents.