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 autolearn=ham autolearn_force=no version=3.4.4 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 103376,fa81b07d7c3d2d7d X-Google-Attributes: gid103376,public X-Google-ArrivalTime: 2003-06-03 12:37:20 PST Path: archiver1.google.com!news1.google.com!newsfeed.stanford.edu!news-spur1.maxwell.syr.edu!news.maxwell.syr.edu!out.nntp.be!propagator2-sterling!in.nntp.be!news.grnet.gr!news.ntua.gr!not-for-mail From: "Papastefanos Serafeim" Newsgroups: comp.lang.ada Subject: Re: Random numbers in tasks Date: Tue, 3 Jun 2003 22:37:18 +0300 Organization: National Technical University of Athens, Greece Message-ID: References: <3EDAC482.40604@attbi.com> <3EDBB85D.3050505@attbi.com> NNTP-Posting-Host: athe530-e106.otenet.gr X-Trace: ulysses.noc.ntua.gr 1054669039 92264 212.205.247.106 (3 Jun 2003 19:37:19 GMT) X-Complaints-To: usenet@ulysses.noc.ntua.gr NNTP-Posting-Date: Tue, 3 Jun 2003 19:37:19 +0000 (UTC) X-Priority: 3 X-MSMail-Priority: Normal X-Newsreader: Microsoft Outlook Express 6.00.2800.1158 X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1165 Xref: archiver1.google.com comp.lang.ada:38546 Date: 2003-06-03T22:37:18+03:00 List-Id: Thank you "Robert I. Eachus" 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; >