comp.lang.ada
 help / color / mirror / Atom feed
From: gautier_niouzes@hotmail.com (Gautier)
Subject: Re: Discrete random with given distribution ?
Date: 14 Jun 2002 03:01:32 -0700
Date: 2002-06-14T10:01:32+00:00	[thread overview]
Message-ID: <17cd177c.0206140201.17fb3eeb@posting.google.com> (raw)
In-Reply-To: aea288$cen$1@dolly.uninett.no

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;



  parent reply	other threads:[~2002-06-14 10:01 UTC|newest]

Thread overview: 23+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2002-06-13 12:10 Discrete random with given distribution ? Reinert Korsnes
2002-06-13 13:35 ` Larry Kilgallen
2002-06-13 12:49   ` Reinert Korsnes
2002-06-13 14:25 ` Wes Groleau
2002-06-13 16:10   ` Kevin Rigotti
2002-06-13 16:33   ` Jeffrey Carter
2002-06-13 22:23     ` Wes Groleau
2002-06-14  8:01     ` Gautier
2002-06-13 18:01   ` tmoran
2002-06-14  7:20     ` Reinert Korsnes
2002-06-14 18:49       ` Wes Groleau
2002-06-14 21:12         ` tmoran
2002-06-16 22:12           ` Wes Groleau
2002-06-17  5:01             ` tmoran
2002-06-17 16:15               ` Wes Groleau
2002-06-17 18:17                 ` tmoran
2002-06-18 19:22                   ` Wes Groleau
2002-06-19  0:34                     ` tmoran
2002-06-19  0:53                       ` tmoran
2002-06-19  5:13                       ` Robert I. Eachus
2002-06-14 10:01 ` Gautier [this message]
  -- strict thread matches above, loose matches on Subject: below --
2002-06-14 22:56 Gautier direct_replies_not_read
2002-06-14 23:10 ` tmoran
replies disabled

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