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.9 required=5.0 tests=BAYES_00,FREEMAIL_FROM autolearn=ham autolearn_force=no version=3.4.4 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 103376,6e2278eaed9619d6 X-Google-Attributes: gid103376,public X-Google-ArrivalTime: 2002-06-14 03:01:32 PST Path: archiver1.google.com!postnews1.google.com!not-for-mail From: gautier_niouzes@hotmail.com (Gautier) Newsgroups: comp.lang.ada Subject: Re: Discrete random with given distribution ? Date: 14 Jun 2002 03:01:32 -0700 Organization: http://groups.google.com/ Message-ID: <17cd177c.0206140201.17fb3eeb@posting.google.com> References: NNTP-Posting-Host: 194.40.39.20 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 8bit X-Trace: posting.google.com 1024048892 445 127.0.0.1 (14 Jun 2002 10:01:32 GMT) X-Complaints-To: groups-abuse@google.com NNTP-Posting-Date: 14 Jun 2002 10:01:32 GMT Xref: archiver1.google.com comp.lang.ada:25931 Date: 2002-06-14T10:01:32+00:00 List-Id: Reinert Korsnes: > Is it under Ada any "natural" way to generate random > elements of enumeration type (for example (a,b,c)) > and with a given non-uniform probability distribution ? Maybe you'll be interested by gnatchopping the following... ____________________________________________________________ Gautier -- http://www.mysunrise.ch/users/gdm/index.htm#Ada NB: For a direct answer, address on the Web site! ---8<--gnatchop---8<--gnatchop---8<--gnatchop---8<--gnatchop---8<-- --From: Reinert Korsnes (reinert.korsnes@chello.no) --Subject: Discrete random with given distribution ? --Newsgroups: comp.lang.ada --View this article only --Date: 2002-06-13 05:25:17 PST --Hi, --Is it under Ada any "natural" way to generate random --elements of enumeration type (for example (a,b,c)) --and with a given non-uniform probability distribution ? --I.e. so for example "a" is produced by probability 1/2 --and "b" and "c" both with probability 1/4 ? generic type A_float is digits <>; type Thing is (<>); type Proba_array is array(Thing) of A_float; with function Uniform_random return A_float; function Finite_distributed_random(proba: Proba_array) return Thing; function Finite_distributed_random(proba: Proba_array) return Thing is subtype AF01 is A_float range 0.0 .. 1.0; U: constant AF01:= Uniform_random; p: AF01:= 0.0; Probabilities_dont_sum_up_to_1: exception; begin for th in Thing loop p:= p + AF01(proba(th)); if U < p then return th; end if; end loop; if (p-1.0) > A_float'epsilon then raise Probabilities_dont_sum_up_to_1; end if; return Thing'last; end Finite_distributed_random; with Finite_distributed_random; with Ada.Text_IO; use Ada.Text_IO; with Ada.Float_Text_IO; use Ada.Float_Text_IO; with Ada.Numerics.Float_Random; procedure Test_Finite_distributed_random is G: Ada.Numerics.Float_Random.Generator; function URand return Float is begin return Ada.Numerics.Float_Random.Random(G); end; type ABC is (a,b,c); type Proba_array is array(ABC) of Float; function ABC_Rand is new Finite_distributed_random( A_float => Float, Thing => ABC, Proba_array => Proba_array, Uniform_random => URand); procedure Test( tirages: Positive; proba: Proba_array ) is sample: array(ABC) of Natural; res: ABC; begin Ada.Numerics.Float_Random.Reset(G); sample:= (others => 0); for i in 1..tirages loop res:= ABC_Rand( proba ); sample(res):= sample(res) + 1; end loop; for x in ABC loop Put( ABC'image(x) & " : prob = "); Put( proba(x), 1,4,0); Put( " ; stat = "); Put( Float( sample(x) ) / Float( tirages ), 1,4,0); New_Line; end loop; New_Line; end Test; begin Test( 100_000, (A=> 0.50, B=> 0.25, C=> 0.25) ); Test( 100_000_000, (A=> 0.123, B=> 0.456, C=> 0.421) ); end;