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.3 required=5.0 tests=BAYES_00,FREEMAIL_FROM, REPLYTO_WITHOUT_TO_CC autolearn=no 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-13 07:25:59 PST Path: archiver1.google.com!news1.google.com!newsfeed.stanford.edu!news-spur1.maxwell.syr.edu!news.maxwell.syr.edu!out.nntp.be!propagator2-SanJose!propagator-SanJose!in.nntp.be!news-in-sanjose!cyclone-sf.pbi.net!151.164.30.35!cyclone.swbell.net!bos-service1.ext.raytheon.com!dfw-service2.ext.raytheon.com.POSTED!not-for-mail Message-ID: <3D08AB63.6AF60F95@despammed.com> From: Wes Groleau Reply-To: wesgroleau@despammed.com X-Mailer: Mozilla 4.77 [en] (Windows NT 5.0; U) X-Accept-Language: en,es-MX,es,pt,fr-CA,fr MIME-Version: 1.0 Newsgroups: comp.lang.ada Subject: Re: Discrete random with given distribution ? References: Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit Date: Thu, 13 Jun 2002 09:25:39 -0500 NNTP-Posting-Host: 151.168.144.162 X-Complaints-To: news@ext.ray.com X-Trace: dfw-service2.ext.raytheon.com 1023978358 151.168.144.162 (Thu, 13 Jun 2002 09:25:58 CDT) NNTP-Posting-Date: Thu, 13 Jun 2002 09:25:58 CDT Organization: Raytheon Company Xref: archiver1.google.com comp.lang.ada:25868 Date: 2002-06-13T09:25:39-05:00 List-Id: > 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 ? I don't know what's "natural" but decide for yourself whether either of these are ugly: type Rand_Val is (A1, A2, B, C); Lookup : array (Rand_Val) of Out_Type := (A1 => A, A2 => A, B => B, C => C); package Rand is new Ada.Numerics.Discrete_Random (Rand_Val); .... return Lookup (Rand.Random (Gen)); ------------- -- FR has been created as random float) Threshold : array (Out_Type) of Float := (A => 0.50, B => 0.75, -- previous plus Probability (B) C => 1.00); -- previous plus Probability (C) .... if Threshold (Threshold'Last) /= 1.0 then raise Programmer_Made_A_Mistake; end if; F := FR.Random; Loop_With_Jump_Out: for I in Out_Type loop if F > Threshold (I) then return I; end if; end loop Loop_With_Jump_Out; ----- The first is awkward if the probabilities are complicated. The second could be a pain doing all the addition, but one could easily write a quarter-page program to prompt for the probabilities and output the aggregate text. If the list of items is long, and you want good average speed, use an intermediate type arranged biggest probabilities first, and return Lookup (I); -- Wes Groleau http://freepages.rootsweb.com/~wgroleau