comp.lang.ada
 help / color / mirror / Atom feed
From: rieachus@comcast.net
Subject: Re: Ada.Numerics.Float_Random.Generator question
Date: Mon, 19 Sep 2016 12:07:19 -0700 (PDT)
Date: 2016-09-19T12:07:19-07:00	[thread overview]
Message-ID: <fbb6e1ab-29da-46a8-b618-ba705877bf62@googlegroups.com> (raw)
In-Reply-To: <5db08ea5-c088-4d16-9d88-d058e4bc48df@googlegroups.com>

On Saturday, September 17, 2016 at 3:40:13 PM UTC-4, Andrew Shvets wrote:
 
> This works.  However, as I'm now looking into the Ada.Numerics.Discrete_Random package, I've noticed that it can generate a random value based on the type that is passed in.  This appeals to me and I'd like to do the same for a custom float type (say I want the delta to be 0.01.)
> 
> As Ada.Numerics.Float_Random.Generator is not a generic package, I can't use the same approach.  The best way that I can think of is to create the same result using From and To and then cast the result to the custom float type.
> 
> I hope that there is a better solution to this.  Is there?

Since working has to be better, yes.  First, what you seem to be asking for is a PRNG that selects from a uniform distribution of fixed point values.  To do this take the generic discrete random package, and redeclare it with a fixed point type:

generic
   type Result_Subtype is delta <>;
package My_Fixed_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
   
end My_Fixed_Random;

Now in the body, put an instance of generic discrete random fit to your fixed point type:

package body My_Fixed_Random is
  type Hidden is range Int64(Result_Subtype'First/Result_Subtype'Small)     ..Int64(Result_Subtype'Last/Result_Subtype'Small);

  package Hidden_Pkg is new Ada.Numerics.Discrete_Random(Hidden);

  Complete with the operations from My_Fixed_Random, multiplying by 'Small and converting to Result_Subtype where needed. All done. ;-)  I wish I had gotten this package added to the Numerics section, as you can see it is about a page of very easy code.  But there were lots of other issues that needed resolving, and not enough time to resolve them all.

Now for the evils that I hope you never meet.  With this package, you should generate only numbers that are multiples of 'Small, and each legal value for Result_Subtype should occur with equal probability.  You might have an issue with a value next below 'First, but usually if you have a fixed point type that officially has 2^n - 1 values, you will know what to do to avoid Constraint_Error.  More troubling is that the language rules allow for fixed point types where the specified 'Small or 'Delta does not evenly divide one.  I do think that permission is right--but if you use it, you had better know a lot about numerics.

  parent reply	other threads:[~2016-09-19 19:07 UTC|newest]

Thread overview: 15+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2016-09-17 19:40 Ada.Numerics.Float_Random.Generator question Andrew Shvets
2016-09-17 20:09 ` J-P. Rosen
2016-09-17 20:14   ` Andrew Shvets
2016-09-17 21:01 ` Jeffrey R. Carter
2016-09-17 23:53   ` Andrew Shvets
2016-09-19 19:07 ` rieachus [this message]
2016-09-25 23:41 ` brbarkstrom
2016-09-26 13:04   ` Robert Eachus
2016-09-26 18:48     ` brbarkstrom
2016-09-29  9:42       ` Some Dude
2016-10-01  3:35         ` Robert Eachus
2016-10-01  3:59           ` Paul Rubin
2016-10-01 14:23             ` Robert Eachus
2016-10-01 15:49               ` Dmitry A. Kazakov
2016-10-01 16:44                 ` Robert Eachus
replies disabled

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox