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-02 07:38:58 PST Path: archiver1.google.com!news1.google.com!newsfeed.stanford.edu!news-spur1.maxwell.syr.edu!news.maxwell.syr.edu!newsfeed.icl.net!newsfeed.fjserv.net!feed.news.nacamar.de!news.belwue.de!news-ge.switch.ch!news.grnet.gr!news.ntua.gr!not-for-mail From: "Papastefanos Serafeim" Newsgroups: comp.lang.ada Subject: Re: Random numbers in tasks Date: Mon, 2 Jun 2003 17:35:16 +0300 Organization: National Technical University of Athens, Greece Message-ID: References: <3EDAC482.40604@attbi.com> NNTP-Posting-Host: athe530-l197.otenet.gr X-Trace: ulysses.noc.ntua.gr 1054564732 47296 212.205.210.197 (2 Jun 2003 14:38:52 GMT) X-Complaints-To: usenet@ulysses.noc.ntua.gr NNTP-Posting-Date: Mon, 2 Jun 2003 14:38:52 +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:38353 Date: 2003-06-02T17:35:16+03:00 List-Id: 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); type Generator_Pointer is access Rnd_Num.Generator; protected Gen_Source is procedure Initialize_Generator ( Pointer : in out Generator_Pointer ); private Init: Integer := 10; end Gen_Source; protected body Gen_Source is procedure Initialize_Generator ( Pointer : in out Generator_Pointer ) is begin Pointer := new Rnd_Num.Generator; Rnd_Num.Reset(Pointer.All, Init); Init := Init + 10; 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); begin return Rnd_Num.Random(Per_Task.Value.All); end Give_Rnd; end Rnds; It compiles fine but when I try to call the function Give_Rnd(...) from inside a task it always returns the same result - Am I doing something wrong ? "Robert I. Eachus" wrote in message news:3EDAC482.40604@attbi.com... > Papastefanos Serafeim wrote: > > There is a small problem in that solution, I cannot just > > declare the generator in the task type: There is a function in > > a different package that is used to return a random > > number in a given range and that function is used at > > other places too... > > There is magic in Ada for especially this purpose. It is in Annex C, so > it is possible that some compilers don't support it. Look at C.7.2 The > Package Task_Attributes. Here is an example and output using GNAT. > Notice that this version uses a fixed seed for each different task. If > you want different seeds for every run modify the body of > My_Float_Random.Gen_Source. But notice that just using a call to get > seeds based on the clock may not be sufficient, unless you put a delay in. > > with Ada.Task_Attributes; > with Ada.Numerics.Float_Random; > package My_Float_Random is > function Random return Ada.Numerics.Float_Random.Uniformly_Distributed; > end My_Float_Random; > > package body My_Float_Random is > type Generator_Pointer is access Ada.Numerics.Float_Random.Generator; > > protected Gen_Source is > procedure Initialize_Generator(Pointer: in out Generator_Pointer); > private > Init: Integer := 10; > end Gen_Source; > > protected body Gen_Source is > procedure Initialize_Generator > (Pointer: in out Generator_Pointer) is > begin > Pointer := new Ada.Numerics.Float_Random.Generator; > Ada.Numerics.Float_Random.Reset(Pointer.all, Init); > Init := Init + 10; > 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; > > with Ada.Text_IO; > with My_Float_Random; > procedure Test_Task_Random is > > package Rand_IO is new Ada.Text_IO.Float_IO(Float); > > task type Rand_Task (ID: Integer) is end Rand_Task; > > type Task_Pointer is access Rand_Task; > TP : Task_Pointer; > > protected IO_Serialize is > procedure Write_Line(S: in String); > end IO_Serialize; > > protected body IO_Serialize is > procedure Write_Line (S: in String) > is begin Ada.Text_IO.Put_Line(S); end Write_Line; > end IO_Serialize; > > task body Rand_Task is > Rand_String: String(1..10); > begin > for I in 1..10 loop > Rand_IO.Put(Rand_String, My_Float_Random.Random, 7, 0); > IO_Serialize.Write_Line( "Task" & Integer'Image(ID) & > " Random number: " & Rand_String); > end loop; > end Rand_Task; > > begin > > for I in 1..10 loop > TP := new Rand_Task(I); > end loop; > > end Test_Task_Random; > > E:\Ada\Random>test_task_random > test_task_random > Task 1 Random number: 0.7443592 > Task 1 Random number: 0.1400007 > Task 1 Random number: 0.9659407 > Task 1 Random number: 0.6070659 > ... > Task 6 Random number: 0.3006703 > Task 7 Random number: 0.5094006 > Task 8 Random number: 0.9858680 > Task 10 Random number: 0.2958182 > Task 9 Random number: 0.7662218 > Task 10 Random number: 0.4117813 > Task 9 Random number: 0.9709954 > Task 10 Random number: 0.2093689 > Task 9 Random number: 0.3808535 > Task 10 Random number: 0.6235309 > > > > >