comp.lang.ada
 help / color / mirror / Atom feed
From: smithd@software.org (Doug Smith)
Subject: Re: looking for Ada random number generator (source posted)
Date: 21 Mar 91 16:35:31 GMT	[thread overview]
Message-ID: <1991Mar21.163531.25159@software.org> (raw)
In-Reply-To: 1991Mar21.141116.23895@software.org

> -- > CML8@ROBIN.CS.UOFS.EDU  asks  (UNIX)
> -- > One of the teachers in my department is looking for an random number generator
> -- > written in Ada.  He only needs it for integers, but a generic one would be fine too.
>  
I have been using this generator extensively and it seems to work fine.  It
is however intended for 32 bit machines, as indicated by the CACM articles
referenced.  I highly recommend these concise articles which should answer
almost any question you might have.

If you don't like the generic form of the random number generator, you can
change it easily enough.  I find that having a different generator instantiated
in carefully selected sections of code can maintain a deterministic behavior
despite debugging and enhancement.


**************************** Cut Here **************************************
-- CACM, October 1988, pp. 1192-1201.
-- CACM, January 1990, pp. 87-88.
-- Implemented in Ada by D. Douglas Smith

generic
    -- To be a full period generator, Multiplier must be a primitive root
    --     of the Prime_Modulus.  This is not checked for, and if not a
    --     primitive root, then the Multiplier will generate only a subset
    --     (partition) of the Positive numbers depending on the Initial
    --     seed.
    -- Also, 2 <= Multiplier < Prime_Modulus and will otherwise raise
    --     Program_Error during elaboration;
    -- This "efficient" implementation requires
    --     (Prime_Modulus mod Multiplier) < (Prime_Modulus / Multiplier)
    --     and will otherwise raise Program_Error during elaboration;

    Prime_Modulus : in Positive := Positive'Last; -- 2**31 -1 = Mersenne prime;
    Multiplier    : in Positive := 7**5;
    Initial_Seed  : in Positive := 40;

package Random is

    -- Note that 0 < Next_Positive < Prime_Modulus.
    function Next_Positive return Positive;
        -- (Multiplier * Seed) mod Prime_Modulus

end Random;


package body Random is
    a : constant Integer := Multiplier;
    m : constant Integer := Prime_Modulus;
    q : constant Integer := m/a;
    r : constant Integer := m mod a;

    seed : Integer := Initial_Seed;

    function Next_Positive return Positive is
    begin
        seed := a *(seed mod q) - r*(seed / q);
        if seed <= 0 then
            seed := seed + m;
        end if;

        return seed;
    end Next_Positive;

begin
    if Multiplier < 2                                                or else
       Multiplier >= Prime_Modulus                                   or else
      (Prime_Modulus mod Multiplier) >= (Prime_Modulus / Multiplier)    then
        -- This implementation will not work.
        raise Program_Error;
    end if;
end Random;
**************************** Cut Here **************************************

      reply	other threads:[~1991-03-21 16:35 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
1991-03-21 14:11 looking for Ada random number generator (source posted) Alex Blakemore
1991-03-21 16:35 ` Doug Smith [this message]
replies disabled

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