comp.lang.ada
 help / color / mirror / Atom feed
From: Niklas Holsti <niklas.holsti@tidorum.invalid>
Subject: Re: Random number generation
Date: Thu, 30 Dec 2010 13:34:23 +0200
Date: 2010-12-30T13:34:23+02:00	[thread overview]
Message-ID: <8o3920Fn0vU1@mid.individual.net> (raw)
In-Reply-To: <864o9vbkwz.fsf@gareth.avalon.lan>

Mart van de Wege wrote:
> Beginner's question: I'm trying to implement a function that rolls a
> number of dice and then adds a modifier. Somehow, it produces the same
> number every time. I can't see where I'm going wrong.
> 
> Here's the proof of concept code:
> 
> with Ada.Numerics.Discrete_Random,Ada.Integer_Text_IO;
> use Ada.Integer_Text_IO;
> 
> procedure Rolltest is
>    function Roll ( Number : in Positive;
> 		   Size : in Positive;
> 		   Modifier : in Integer := 0 ) return Integer is 
>       subtype Die_Size is Positive range 2..Size;
>       package Die is new Ada.Numerics.Discrete_Random( Die_Size );
>       G : Die.Generator;
>       Result : Integer;
>       Temp : Integer;
>    begin
>       Die.Reset(G);

This Reset operation is specified (in RM A.5.2(34)) to set the state of 
the generator G to a "time-dependent" state.

If every call of Roll is returning the same results, perhaps the calls 
happen so rapidly that whatever discrete "time" source Reset uses is the 
same on each call, thus setting G to the same state on each call.

Ordinarily I would suggest to call Reset just once, and then let the 
state of the generator evolve without Resets over all calls of Roll, but 
this cannot be done here because G is a local variable in Roll, and it 
must be so since the generic actual parameter depends on the Size 
parameter of Roll.

Perhaps you should make another random Integer number generator IntGen, 
local to Rolltest but not to Roll, and use the generated Integer numbers 
to initialize Roll.G by a Reset call that has the random Integer as the 
second, state-determining "Initiator" parameter. The IntGen generator 
should be Reset in Rolltest, not in Roll, or left in its default initial 
state.

If you leave a random-number generator with its default initial state 
(no Reset call), you get the same random-number sequence on every execution.

If you use the single-parameter Reset (G) procedure, you get a sequence 
that is different for different executions, providing that the Resets 
are far enough apart in time to make a difference.

>       Result := 0;
>       for I in 1..Number loop
> 	 Temp := Die.Random(G);
> 	 Result := Result + Temp;
>       end loop;
>       Result := Result + Modifier;
>       return Result;
>    end Roll;
> begin
>    for I in 1..10 loop
>       Put(Roll( Number => 3, Size => 6 ));
>    end loop;
> end Rolltest;
> 
> Anyone care to at least point me to some documentation that explains
> what I'm doing wrong? 
> 
> Mart
> 


-- 
Niklas Holsti
Tidorum Ltd
niklas holsti tidorum fi
       .      @       .



  parent reply	other threads:[~2010-12-30 11:34 UTC|newest]

Thread overview: 57+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2010-12-30 10:43 Random number generation Mart van de Wege
2010-12-30 10:54 ` Thomas Løcke
2010-12-30 12:11   ` Mart van de Wege
2010-12-30 11:34 ` Niklas Holsti [this message]
2010-12-30 11:53   ` Georg Bauhaus
2010-12-30 12:25     ` Mart van de Wege
2010-12-30 15:29       ` Georg Bauhaus
2010-12-30 15:37         ` Mart van de Wege
2010-12-30 11:51 ` Brian Drummond
2010-12-30 12:16   ` Mart van de Wege
2010-12-30 13:04 ` Dmitry A. Kazakov
2010-12-30 13:22   ` Niklas Holsti
2010-12-30 13:39     ` Dmitry A. Kazakov
2010-12-30 13:30   ` Mart van de Wege
2010-12-31  3:14 ` Gene
  -- strict thread matches above, loose matches on Subject: below --
2010-07-13 12:45 tonyg
2010-07-13 12:50 ` Jacob Sparre Andersen
2010-07-13 12:58 ` Dmitry A. Kazakov
2010-07-13 13:17 ` Thomas Løcke
2010-07-13 16:07 ` Jeffrey R. Carter
2010-07-13 20:33   ` John B. Matthews
2010-07-13 23:02     ` Jeffrey R. Carter
2010-07-14  4:42       ` John B. Matthews
2010-07-15 19:01         ` tonyg
2003-09-26  7:14 random " christoph.grein
2003-09-26  7:00 Andrew
2003-09-26  7:35 ` tmoran
2003-09-26 17:58   ` Andrew
2003-09-26 19:25   ` Andrew
2003-09-26 19:35     ` chris
2003-09-26 21:44     ` tmoran
2003-09-27  1:40     ` Robert I. Eachus
2003-09-27  4:48       ` Andrew
1997-12-19  0:00 Mok-kong Shen
1998-01-02  0:00 ` Mok-kong Shen
1998-01-02  0:00   ` Robert Dewar
1996-10-13  0:00 Random Number Generation parker
1996-10-13  0:00 ` Robert Dewar
1996-10-14  0:00 ` Robert A Duff
1996-10-10  0:00  Dr J Parker
1996-10-10  0:00  Dr J Parker
1996-10-12  0:00 ` Keith Thompson
1996-10-12  0:00 ` Geert Bosch
1996-10-02  0:00  Dr J Parker
1996-10-03  0:00 ` Mats Weber
1996-10-07  0:00 ` Geert Bosch
1996-09-23  0:00 Nigel J. Tracey
1996-09-23  0:00 ` Tucker Taft
1996-10-02  0:00   ` Nigel J. Tracey
1996-10-02  0:00   ` Robert I. Eachus
1996-10-03  0:00   ` Nigel J. Tracey
1996-09-25  0:00 ` James_Rogers
1996-09-26  0:00   ` Dale Stanbrough
1996-10-01  0:00   ` Robert I. Eachus
1996-09-30  0:00 `  Dr J Parker
1996-10-01  0:00   ` Tucker Taft
1996-10-01  0:00     ` Keith Thompson
replies disabled

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