R. Random - JulTob/Ada GitHub Wiki
with Ada.Numerics.Discrete_Random;
...
subtype Faces is Positive range 1 .. 20;
package RandInt is new
Ada.Numerics.Discrete_Random(Result_Subtype => Faces);
-- package RandInt is new
Ada.Numerics.Discrete_Random(Faces);
-- Creates a custom object-package
-- use RandInt; --?
...
Dice: RandInt.Generator;
Face: Faces:= 1;
...
Randint.Reset(Dice);
Randint.Reset( Gen => Dice);
Face:= Randint.Random(Gen => Dice);
Face:= Randint.Random(Dice);
subtype Values is Natural range 10..50;
package Random_Values is new Ada.Numerics.Discrete_Random(Result_Subtype => Random_Values);
Dice: Random_Values.Generator;
begin
Random_Values.Reset(Gen => Dice);
return Natural(Random_Values.Random(Gen => Dice));
Seed: Ada.Numerics.Float_Random.Generator;
begin
Ada.Numerics.Float_Random.Reset(Seed);
return ( Ada.Numerics.Float_Random.Random(Seed) );
-- in the range 0.0 .. 1.0
Discrete random
-- die roll simulation
WITH Text_IO; USE Text_IO;
with ada.Integer_Text_IO; USE ada.Integer_Text_IO;
with Ada.Numerics.Discrete_Random;
procedure DieRoll is
subtype DieT is Integer range 1 .. 6;
package Random_Die is new Ada.Numerics.Discrete_Random (DieT);
use Random_Die;
G : Generator;
Die : DieT;
begin
Reset (G); -- Start the generator in a unique state in each run
-- Roll a die
Die := Random(G);
put("You rolled a "); put(Die, 1); new_line;
end DieRoll;