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-01 20:29:39 PST Path: archiver1.google.com!news1.google.com!newsfeed.stanford.edu!logbridge.uoregon.edu!arclight.uoregon.edu!wn13feed!worldnet.att.net!204.127.198.203!attbi_feed3!attbi_feed4!attbi.com!sccrnsc04.POSTED!not-for-mail Message-ID: <3EDAC482.40604@attbi.com> From: "Robert I. Eachus" User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.0; en-US; rv:1.0.2) Gecko/20021120 Netscape/7.01 X-Accept-Language: en-us, en MIME-Version: 1.0 Newsgroups: comp.lang.ada Subject: Re: Random numbers in tasks References: Content-Type: text/plain; charset=us-ascii; format=flowed Content-Transfer-Encoding: 7bit NNTP-Posting-Host: 24.62.164.137 X-Complaints-To: abuse@attbi.com X-Trace: sccrnsc04 1054524573 24.62.164.137 (Mon, 02 Jun 2003 03:29:33 GMT) NNTP-Posting-Date: Mon, 02 Jun 2003 03:29:33 GMT Organization: AT&T Broadband Date: Mon, 02 Jun 2003 03:29:33 GMT Xref: archiver1.google.com comp.lang.ada:38299 Date: 2003-06-02T03:29:33+00:00 List-Id: 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