From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on polar.synack.me X-Spam-Level: X-Spam-Status: No, score=-0.8 required=5.0 tests=BAYES_00,INVALID_DATE autolearn=no autolearn_force=no version=3.4.4 Path: utzoo!utgpu!news-server.csri.toronto.edu!rpi!zaphod.mps.ohio-state.edu!sol.ctr.columbia.edu!emory!gatech!mcnc!uvaarpa!software.org!smithd From: smithd@software.org (Doug Smith) Newsgroups: comp.lang.ada Subject: Re: looking for Ada random number generator (source posted) Keywords: Ada random variates source Message-ID: <1991Mar21.163531.25159@software.org> Date: 21 Mar 91 16:35:31 GMT References: <1991Mar21.141116.23895@software.org> Sender: usenet@software.org (Usenet News/Mail Support) Organization: spd List-Id: > -- > 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 **************************************