Reputation: 23
Adam.Numerics.Float_Random generates Float values for 0 - 1.0
but i want to generate a Float value between a user defined range. Like Min := 100.0
and Max := 200.0
. Something like this:
with Ada.Text_IO, Ada.Integer_Text_IO, Ada.Float_Text_IO;
use Ada.Text_IO, Ada.Integer_Text_IO, Ada.Float_Text_IO;
with Ada.Numerics.Float_Random; use Ada.Numerics.Float_Random;
procedure test1 is
Min, Max, Answer : Float;
subtype Custom is Float range Min .. Max;
package New_Package is new Ada.Numerics.Float_Random(Custom);
use New_Package;
RF : Generator;
begin
Reset(RF);
Get(Min);
Get(Max);
Answer := Random(RF);
Put(Answer, Fore => 1, Aft => 2, Exp => 0);
end test1;
Upvotes: 2
Views: 343
Reputation: 1641
Looks like homework... I usually don't give the answer for such a task :)
First of all, you should get a warning when compiling your code as Min and Max are uninitialized when building your subtype.
To avoid this, the subtype must be declared after values are known. To do this, declare your subtype inside a declare block.
About the random generation, getting a value inside your range is only a matter of translating and scaling.
In the end, I would do something like this.
with Ada.Float_Text_IO; use Ada.Float_Text_IO;
with Ada.Numerics.Float_Random; use Ada.Numerics.Float_Random;
procedure test1 is
Min, Max : Float;
RF : Generator;
begin
Reset(RF);
Get(Min);
Get(Max);
declare
subtype Custom is Float range Min .. Max;
Answer : Custom;
begin
Answer := (Random(RF) * (Max - Min)) + Min;
Put(Answer, Fore => 1, Aft => 2, Exp => 0);
end;
end test1;
In this particular case, the subtype is quite useless here.
Upvotes: 2