From: "Papastefanos Serafeim" <serafeim@otenet.gr>
Subject: Re: Random numbers in tasks
Date: Tue, 3 Jun 2003 22:37:18 +0300
Date: 2003-06-03T22:37:18+03:00 [thread overview]
Message-ID: <bbitde$2q38$1@ulysses.noc.ntua.gr> (raw)
In-Reply-To: 3EDBB85D.3050505@attbi.com
Thank you
"Robert I. Eachus" <rieachus@attbi.com> wrote in message
news:3EDBB85D.3050505@attbi.com...
> Papastefanos Serafeim wrote:
> > Thank you for the big answer... I tried your program and
> > it works just fine, but when I try to convert it to my
> > requirements it is not working:
>
> > I want to use it with discrete rundom numbers with a
> > variable range... I have made changes to your program
> > and now looks like this ->
> > with Ada.Numerics.Discrete_Random;
> > with Ada.Task_Attributes;
> > package body Rnds is
> > function Give_Rnd (
> > L : Integer;
> > U : Integer;
> > ) return Integer is
> > subtype Rng is Integer range L..U;
> > package Rnd_Num is new Ada.Numerics.Discrete_Random (Rng);
>
> Never instantiate a random number package inside a function. As you
> have noticed, the results are not what you want. Here is a version of
> what I gave you before, but with the init generator settings based on
> one clock call.
>
> with Ada.Numerics.Discrete_Random;
> package body My_Float_Random is
> type Generator_Pointer is access Ada.Numerics.Float_Random.Generator;
>
> package Int_Rand is new Ada.Numerics.Discrete_Random(Positive);
>
> protected Gen_Source is
> procedure Initialize_Generator(Pointer: in out Generator_Pointer);
> private
> Int_Gen: Int_Rand.Generator;
> Initial: Boolean := True;
> end Gen_Source;
>
> protected body Gen_Source is
> procedure Initialize_Generator
> (Pointer: in out Generator_Pointer) is
> begin
> if Initial then
> Int_Rand.Reset(Int_Gen);
> Initial := False; -- use time of day once
> end if;
> Pointer := new Ada.Numerics.Float_Random.Generator;
> Ada.Numerics.Float_Random.Reset
> (Pointer.all, Int_Rand.Random(Int_Gen));
> end Initialize_Generator;
> end Gen_Source;
>
> function Initialize return Generator_Pointer is
> GP : Generator_Pointer;
> begin
> Gen_Source.Initialize_Generator(GP);
> return GP;
> end Initialize;
>
> package Per_Task is new
> Ada.Task_Attributes(Generator_Pointer, Initialize);
>
> function Random return
> Ada.Numerics.Float_Random.Uniformly_Distributed is
> begin
> return Ada.Numerics.Float_Random.Random(Per_Task.Value.all);
> end Random;
>
> end My_Float_Random;
>
prev parent reply other threads:[~2003-06-03 19:37 UTC|newest]
Thread overview: 11+ messages / expand[flat|nested] mbox.gz Atom feed top
2003-05-31 22:01 Random numbers in tasks Papastefanos Serafeim
2003-06-01 2:09 ` David C. Hoos
2003-06-01 3:42 ` Bobby D. Bryant
2003-06-01 18:05 ` Papastefanos Serafeim
2003-06-01 21:31 ` tmoran
2003-06-02 2:24 ` Bobby D. Bryant
2003-06-02 3:29 ` Robert I. Eachus
2003-06-02 5:32 ` Bobby D. Bryant
2003-06-02 14:35 ` Papastefanos Serafeim
2003-06-02 20:50 ` Robert I. Eachus
2003-06-03 19:37 ` Papastefanos Serafeim [this message]
replies disabled
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox