comp.lang.ada
 help / color / mirror / Atom feed
* Strange results with random numbers with Gnat 3.10
@ 1998-01-07  0:00 Andreas Almroth
  1998-01-07  0:00 ` Mats Weber
  1998-01-08  0:00 ` Philip Brashear
  0 siblings, 2 replies; 5+ messages in thread
From: Andreas Almroth @ 1998-01-07  0:00 UTC (permalink / raw)



Hi folks,
I was wandering through the excellent online RM Ada95 at
www.adahome.com and read about the random number facility
(A.5.2).
I compiled and ran the dice example program, and everything
seemed OK. But just as a curiousity I wanted to see how the
numbers were actually distributed, so I changed the program
to loop one million times, and then present the frequency.
To my big suprise this was the result;

Number  Frequency
=================
  1        249594
  2        167104
  3        166985
  4        166687
  5        166621
  6         83009

A bit odd distribution, if I may say so... Is this a feature
of Gnat, or?

The reference manual clearly states that;

" 		Implementation Requirements
(41)
	A sufficiently long sequence of random numbers obtained by
successive calls to Random is approximately uniformly distributed over
the range of the result subtype."

So, the result should be different...

Any comments, or I'm making the wrong conclusions?

Of course, one could use 2..5 and skip the min and max values, but
why should I do that, if the Ada95 language enviroment should handle it?

Regards,
         Andreas Almroth
-- 
============================================================
A3CE, Advanced Computer Engineering
System Integration Consultants
Phone: +46-18-100 790
============================================================
Any opinions expressed in this letter, are mine, and the 
company's, because I own it... :-)




^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: Strange results with random numbers with Gnat 3.10
  1998-01-07  0:00 Strange results with random numbers with Gnat 3.10 Andreas Almroth
@ 1998-01-07  0:00 ` Mats Weber
  1998-01-08  0:00   ` Andreas Almroth
  1998-01-08  0:00 ` Philip Brashear
  1 sibling, 1 reply; 5+ messages in thread
From: Mats Weber @ 1998-01-07  0:00 UTC (permalink / raw)
  To: Andreas Almroth


> I compiled and ran the dice example program, and everything
> seemed OK. But just as a curiousity I wanted to see how the
> numbers were actually distributed, so I changed the program
> to loop one million times, and then present the frequency.

Please show us the code. It's very easy to make a rounding mistake, which is
what seems to be the case here.




^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: Strange results with random numbers with Gnat 3.10
  1998-01-07  0:00 ` Mats Weber
@ 1998-01-08  0:00   ` Andreas Almroth
  1998-01-08  0:00     ` Mats Weber
  0 siblings, 1 reply; 5+ messages in thread
From: Andreas Almroth @ 1998-01-08  0:00 UTC (permalink / raw)



Mats Weber wrote:
> 
> > I compiled and ran the dice example program, and everything
> > seemed OK. But just as a curiousity I wanted to see how the
> > numbers were actually distributed, so I changed the program
> > to loop one million times, and then present the frequency.
> 
> Please show us the code. It's very easy to make a rounding mistake, which is
> what seems to be the case here.

Ok, below is the program. I've double checked the code, but
maybe I'm not up to it, I can't find were the problem possibly 
could be in this code.

-- dice game
with Text_Io;
use Text_Io;
with Ada.Numerics.Discrete_Random;
procedure Dice_Game is
   subtype Die is Integer range 1 .. 6;
   subtype Dice is Integer range 2*Die'First .. 2*Die'Last;

   type Pins is array(1..6) of Integer range 0..1000000;

   package Random_Die is new Ada.Numerics.Discrete_Random (Die);
   package Dice_Io is new Integer_Io(Integer);
   use Random_Die;
   use Dice_Io;
   G : Generator;
   D : Die;
   P : Pins;
begin
   for I in 1..6 loop
      P(I) := 0;
   end loop;

   Reset (G);
   for I in 1..1000000 loop
      D := Random(G);
      P(D) := P(D) + 1;
   end loop;

   for I in 1..6 loop
      Put(P(I));
      New_Line;
   end loop;
end Dice_Game;



Regards,
         Andreas Almroth




^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: Strange results with random numbers with Gnat 3.10
  1998-01-07  0:00 Strange results with random numbers with Gnat 3.10 Andreas Almroth
  1998-01-07  0:00 ` Mats Weber
@ 1998-01-08  0:00 ` Philip Brashear
  1 sibling, 0 replies; 5+ messages in thread
From: Philip Brashear @ 1998-01-08  0:00 UTC (permalink / raw)



In article <34B3F618.DC9@uppsala.mail.telia.com>,
Andreas Almroth  <andreas.almroth@uppsala.mail.telia.com> wrote:
[... trying to adapt dice example in RM A5.2 ...]
>To my big suprise this was the result;
>
>Number  Frequency
>=================
>  1        249594
>  2        167104
>  3        166985
>  4        166687
>  5        166621
>  6         83009


Are you sure you used the example in the RM?  It's a two-dice example,
so the possible values are 2 .. 12, not 1 .. 6.  If you modified the
example to produce single-die results, are you sure you got the code
right?  (Honestly, though it seems easy, it's not trivial.)

Could you post your source?

(I wrote a two-dice version for the Ada Compiler Evaluation System, and,
as I recall, the earlier versions of GNAT produced acceptable results.)

Phil Brashear
EDS




^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: Strange results with random numbers with Gnat 3.10
  1998-01-08  0:00   ` Andreas Almroth
@ 1998-01-08  0:00     ` Mats Weber
  0 siblings, 0 replies; 5+ messages in thread
From: Mats Weber @ 1998-01-08  0:00 UTC (permalink / raw)



> Ok, below is the program. I've double checked the code, but
> maybe I'm not up to it, I can't find were the problem possibly
> could be in this code.

You are right, your code is correct and GNAT is wrong.

in the file a-nudira.ads, the line

   Offs : constant Flt := RstF + 0.5;

should be replaced with

   Offs : constant Flt := RstF - 0.5;




^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~1998-01-08  0:00 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
1998-01-07  0:00 Strange results with random numbers with Gnat 3.10 Andreas Almroth
1998-01-07  0:00 ` Mats Weber
1998-01-08  0:00   ` Andreas Almroth
1998-01-08  0:00     ` Mats Weber
1998-01-08  0:00 ` Philip Brashear

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