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=-1.3 required=5.0 tests=BAYES_00,INVALID_MSGID autolearn=no autolearn_force=no version=3.4.4 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 103376,13a53a863bda3aeb X-Google-Attributes: gid103376,public From: eachus@spectre.mitre.org (Robert I. Eachus) Subject: Re: Source for Random Number Generator Date: 1997/02/07 Message-ID: #1/1 X-Deja-AN: 215191440 references: <5d8b95$oec@zeus.orl.mmc.com> organization: The Mitre Corp., Bedford, MA. newsgroups: comp.lang.ada Date: 1997-02-07T00:00:00+00:00 List-Id: Following up my own posting, how gauche. Oh, well I didn't want to mislead anyone... > with Ada.Numerics.Float_Random; use Ada.Numerics.Float_Random; > function Biased_Random(Gen: in Generator) return Float is > begin > return Random(Gen) * 0.9 + 0.1; > end Biased_Random; Ask for quick and dirty and get it. I probably should have put a smiley face on this. > Now if you will settle for not so quick and dirty, I would > recommend: > with Ada.Numerics.Float_Random; > generic > type Result_Type is digits <>; > Gen: in out Ada.Numerics.Float_Random.Generator; > function Generalized_Random return Result_Type; Do not instantiate this with an unconstrained floating-point type. It will break, and I can see no reason slow things down to deal with such a bogus choice. But I probably should have put in a comment. > function Generalized_Random return Result_Type is > Temp: Result_Type; > begin > Temp := Result_Type(Float(Result_Type'First) + > Float(Result_Type'Last-Result_Type'First) * > Ada.Numerics.Float_Random.Random(Gen)); > return Temp; -- Temp is only used for expository purposes. On some hardware it may -- be better to just say: > return Result_Type(Float(Result_Type'First) + > Float(Result_Type'Last-Result_Type'First) * > Ada.Numerics.Float_Random.Random(Gen)); -- But it depends on your compiler's inlining policy. > exception when others => return Generalized_Random; > -- rounding errors can result in a value of Temp outside the result > -- subtype when the bounds are not model numbers. > end Generalized_Random; -- This comment is technically not true if the compiler doesn't -- support the numerics annex or you are not running in strict mode. -- But you still want the exception handler there. with Ada.Numerics.Float_Random; with Generalized_Random; with Text_IO; procedure Test_GR is subtype Result_Subtype is Float range 0.1..1.0; My_Gen: Ada.Numerics.Float_Random.Generator; function Rand is new Generalized_Random(Result_Subtype, My_Gen); -- add pragma Inline for Rand here. package Floating_IO is new Text_IO.Float_IO(Result_Subtype); begin Text_IO.New_Line; for I in 1..10 loop for J in 1..6 loop Floating_IO.Put(Rand,3,9,0); end loop; Text_IO.New_Line; end loop; end Test_GR; -- Robert I. Eachus with Standard_Disclaimer; use Standard_Disclaimer; function Message (Text: in Clever_Ideas) return Better_Ideas is...