comp.lang.ada
 help / color / mirror / Atom feed
* "Constraint Error: Range Check" That Does Not Always Occur
@ 2007-11-22  6:42 DarthBob88
  2007-11-22  7:38 ` Damien Carbonne
                   ` (2 more replies)
  0 siblings, 3 replies; 8+ messages in thread
From: DarthBob88 @ 2007-11-22  6:42 UTC (permalink / raw)


I've been trying to write a program that creates four random numbers,
to simulate four dice rolling; a D&D simulator, in fact.
Unfortunately, I occasionally get "raised CONSTRAINT_ERROR : test.adb:
22 range check failed" (line 22 is one of the lines to generate a
random number; it varies between the four). I have been trying to find
information on how to prevent this error, but I can't find anything
about it. I'm extremely new to this language, as in I only started
last week new, so any help would be greatly appreciated. Here follows
my code, warts and all.

with Ada.Text_IO, Ada.Integer_Text_IO;
with Ada.Numerics.Discrete_Random;

procedure test is
	subtype Die is Integer range 1..6;
	--Declare that anything of type Die may be
	--an integer, value between 1 and 6, inclusive, and sod-all else.
	subtype Dice is Integer range 3*Die'First .. 3*Die'Last;
	--Declares that Dice may be anything from 3 to 18.
	package Random_Die is new Ada.Numerics.Discrete_Random (Die);
	use Random_Die;
	--Random generator; produces an integer between 1 and 6
	sum : Integer := 0;
	G: Generator;
     	D1 : Dice;
	D2 : Dice;
	D3 : Dice;
	D4 : Dice;
	--Declare four Dice
begin
	Reset(G);
	D1 := Random(G);
	D2 := Random(G);
	D3 := Random(G);
	D4 := Random(G);
	--I Haven't found the error just yet, but a Constraint_Error
	--Occasionally comes up for the previous 4 lines
	--I haven't been able to find it online, but I presume
	--It's because the generator comes up with a number outside of [1,6]
	sum := D1 + D2 + D3 + D4;
	--Debugging
	Ada.Integer_Text_IO.Put(D1);
	Ada.Integer_Text_IO.Put(D2);
	Ada.Integer_Text_IO.Put(D3);
	Ada.Integer_Text_IO.Put(D4);
	Ada.Integer_Text_IO.Put(sum);
end test;
--compile Ada programs with gnat make <foo>.adb

If it helps any, I'm using the Windows version of GNAT on XP. Thanks
in advance.



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

* Re: "Constraint Error: Range Check" That Does Not Always Occur
  2007-11-22  6:42 "Constraint Error: Range Check" That Does Not Always Occur DarthBob88
@ 2007-11-22  7:38 ` Damien Carbonne
  2007-11-22  9:12   ` anon
                     ` (2 more replies)
  2007-11-22  9:47 ` Stuart
  2007-11-22 14:14 ` Matthew Heaney
  2 siblings, 3 replies; 8+ messages in thread
From: Damien Carbonne @ 2007-11-22  7:38 UTC (permalink / raw)
  To: DarthBob88

DarthBob88 a �crit :
> I've been trying to write a program that creates four random numbers,
> to simulate four dice rolling; a D&D simulator, in fact.
> Unfortunately, I occasionally get "raised CONSTRAINT_ERROR : test.adb:
> 22 range check failed" (line 22 is one of the lines to generate a
> random number; it varies between the four). I have been trying to find
> information on how to prevent this error, but I can't find anything
> about it. I'm extremely new to this language, as in I only started
> last week new, so any help would be greatly appreciated. Here follows
> my code, warts and all.
> 
> with Ada.Text_IO, Ada.Integer_Text_IO;
> with Ada.Numerics.Discrete_Random;
> 
> procedure test is
> 	subtype Die is Integer range 1..6;
> 	--Declare that anything of type Die may be
> 	--an integer, value between 1 and 6, inclusive, and sod-all else.
> 	subtype Dice is Integer range 3*Die'First .. 3*Die'Last;
> 	--Declares that Dice may be anything from 3 to 18.
> 	package Random_Die is new Ada.Numerics.Discrete_Random (Die);
Here you instantiate Discrete_Random with Die.
Change Die to Dice, it should work.
> 	use Random_Die;
> 	--Random generator; produces an integer between 1 and 6
> 	sum : Integer := 0;
> 	G: Generator;
>      	D1 : Dice;
> 	D2 : Dice;
> 	D3 : Dice;
> 	D4 : Dice;
> 	--Declare four Dice
> begin
> 	Reset(G);
> 	D1 := Random(G);
> 	D2 := Random(G);
> 	D3 := Random(G);
> 	D4 := Random(G);
Here, Random should have been instantiated with Dice.
> 	--I Haven't found the error just yet, but a Constraint_Error
> 	--Occasionally comes up for the previous 4 lines
> 	--I haven't been able to find it online, but I presume
> 	--It's because the generator comes up with a number outside of [1,6]
> 	sum := D1 + D2 + D3 + D4;
> 	--Debugging
> 	Ada.Integer_Text_IO.Put(D1);
> 	Ada.Integer_Text_IO.Put(D2);
> 	Ada.Integer_Text_IO.Put(D3);
> 	Ada.Integer_Text_IO.Put(D4);
> 	Ada.Integer_Text_IO.Put(sum);
> end test;
> --compile Ada programs with gnat make <foo>.adb
> 
> If it helps any, I'm using the Windows version of GNAT on XP. Thanks
> in advance.



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

* Re: "Constraint Error: Range Check" That Does Not Always Occur
  2007-11-22  7:38 ` Damien Carbonne
@ 2007-11-22  9:12   ` anon
  2007-11-22  9:56   ` Stephen Leake
  2007-11-26 18:38   ` Adam Beneschan
  2 siblings, 0 replies; 8+ messages in thread
From: anon @ 2007-11-22  9:12 UTC (permalink / raw)


[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #1: Type: text/plain, Size: 3322 bytes --]

-- D? has a type that has the range of 3..18
-- Random return a type that has the range of 1..6
-- So, when Random returns 1..2  => Constraint_Error
-- using D? := Random(G)*3; fixes the problem.
--

with Ada.Text_IO, Ada.Integer_Text_IO;
with Ada.Numerics.Discrete_Random;


procedure test is
	subtype Die is Integer range 1..6;
	--Declare that anything of type Die may be
	--an integer, value between 1 and 6, inclusive, and sod-all else.
	subtype Dice is Integer range 3*Die'First .. 3*Die'Last;
	--Declares that Dice may be anything from 3 to 18.
	package Random_Die is new Ada.Numerics.Discrete_Random (Die);
	use Random_Die;
	--Random generator; produces an integer between 1 and 6
	sum : Integer := 0;
	G: Generator;
     	D1 : Dice;
	D2 : Dice;
	D3 : Dice;
	D4 : Dice;
	--Declare four Dice
begin
	Reset(G);
	D1 := Random(G)*3;
	D2 := Random(G)*3;
	D3 := Random(G)*3; 
	D4 := Random(G)*3;  

	sum := D1 + D2 + D3 + D4;
	--Debugging
	Ada.Integer_Text_IO.Put(D1);
	Ada.Integer_Text_IO.Put(D2);
	Ada.Integer_Text_IO.Put(D3);
	Ada.Integer_Text_IO.Put(D4);
	Ada.Integer_Text_IO.Put(sum);
end test;

In <474531FD.5090401@free.fr>, Damien Carbonne <damien.carbonne@free.fr> writes:
>DarthBob88 a �crit :
>> I've been trying to write a program that creates four random numbers,
>> to simulate four dice rolling; a D&D simulator, in fact.
>> Unfortunately, I occasionally get "raised CONSTRAINT_ERROR : test.adb:
>> 22 range check failed" (line 22 is one of the lines to generate a
>> random number; it varies between the four). I have been trying to find
>> information on how to prevent this error, but I can't find anything
>> about it. I'm extremely new to this language, as in I only started
>> last week new, so any help would be greatly appreciated. Here follows
>> my code, warts and all.
>> 
>> with Ada.Text_IO, Ada.Integer_Text_IO;
>> with Ada.Numerics.Discrete_Random;
>> 
>> procedure test is
>> 	subtype Die is Integer range 1..6;
>> 	--Declare that anything of type Die may be
>> 	--an integer, value between 1 and 6, inclusive, and sod-all else.
>> 	subtype Dice is Integer range 3*Die'First .. 3*Die'Last;
>> 	--Declares that Dice may be anything from 3 to 18.
>> 	package Random_Die is new Ada.Numerics.Discrete_Random (Die);
>Here you instantiate Discrete_Random with Die.
>Change Die to Dice, it should work.
>> 	use Random_Die;
>> 	--Random generator; produces an integer between 1 and 6
>> 	sum : Integer := 0;
>> 	G: Generator;
>>      	D1 : Dice;
>> 	D2 : Dice;
>> 	D3 : Dice;
>> 	D4 : Dice;
>> 	--Declare four Dice
>> begin
>> 	Reset(G);
>> 	D1 := Random(G);
>> 	D2 := Random(G);
>> 	D3 := Random(G);
>> 	D4 := Random(G);
>Here, Random should have been instantiated with Dice.
>> 	--I Haven't found the error just yet, but a Constraint_Error
>> 	--Occasionally comes up for the previous 4 lines
>> 	--I haven't been able to find it online, but I presume
>> 	--It's because the generator comes up with a number outside of [1,6]
>> 	sum := D1 + D2 + D3 + D4;
>> 	--Debugging
>> 	Ada.Integer_Text_IO.Put(D1);
>> 	Ada.Integer_Text_IO.Put(D2);
>> 	Ada.Integer_Text_IO.Put(D3);
>> 	Ada.Integer_Text_IO.Put(D4);
>> 	Ada.Integer_Text_IO.Put(sum);
>> end test;
>> --compile Ada programs with gnat make <foo>.adb
>> 
>> If it helps any, I'm using the Windows version of GNAT on XP. Thanks
>> in advance.




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

* Re: "Constraint Error: Range Check" That Does Not Always Occur
  2007-11-22  6:42 "Constraint Error: Range Check" That Does Not Always Occur DarthBob88
  2007-11-22  7:38 ` Damien Carbonne
@ 2007-11-22  9:47 ` Stuart
  2007-11-22 14:14 ` Matthew Heaney
  2 siblings, 0 replies; 8+ messages in thread
From: Stuart @ 2007-11-22  9:47 UTC (permalink / raw)


"DarthBob88" <darthbob88@gmail.com> wrote in message 
news:86ddf1f4-c7be-4729-8480-bae618b6d296@a39g2000pre.googlegroups.com...
> I've been trying to write a program that creates four random numbers,
> to simulate four dice rolling; a D&D simulator, in fact.
> Unfortunately, I occasionally get "raised CONSTRAINT_ERROR : test.adb:
> 22 range check failed" (line 22 is one of the lines to generate a
> random number; it varies between the four). I have been trying to find
> information on how to prevent this error, but I can't find anything
> about it. I'm extremely new to this language, as in I only started
> last week new, so any help would be greatly appreciated. Here follows
> my code, warts and all.
>
> with Ada.Text_IO, Ada.Integer_Text_IO;
> with Ada.Numerics.Discrete_Random;
>
> procedure test is
> subtype Die is Integer range 1..6;
> --Declare that anything of type Die may be
> --an integer, value between 1 and 6, inclusive, and sod-all else.
> subtype Dice is Integer range 3*Die'First .. 3*Die'Last;
> --Declares that Dice may be anything from 3 to 18.
> package Random_Die is new Ada.Numerics.Discrete_Random (Die);
> use Random_Die;
> --Random generator; produces an integer between 1 and 6
> sum : Integer := 0;
> G: Generator;
>     D1 : Dice;
> D2 : Dice;
> D3 : Dice;
> D4 : Dice;
> --Declare four Dice
> begin
> Reset(G);
> D1 := Random(G);
> D2 := Random(G);
> D3 := Random(G);
> D4 := Random(G);
> --I Haven't found the error just yet, but a Constraint_Error
Are you sure you use Random or is it Random_Die!

Look carefully at how you have declared D1..D4 and compare that with the 
declaration of Random/Random_Die.

One side is expecting to get numbers in the range 3..18, the other to 
produce numbers in the range 1..6!

One way you could have detected this at compile time would have been to make 
the types Die and Dice distinct types rather than subtypes.  Learning to 
make 'correct' use of types and subtypes (to detect programming errors 
earlier) is a major part of the Ada paradigm.  At the start it seems an 
awful hassle - but you should find it becomes more natural and creates a 
robust thinking strategy when designing programs.

(Don't forget that Ada allows you to do explicit type conversion when you 
really do want to change from the view of having the score from one die to 
the score from several dice.  The extra program text makes it clear to 
readers/maintainers that the change of view is intentional.  Again this is 
part of the Ada ethos of making programs easier for reviewers/maintainers 
rather than minimize the amount of typing the original author has to do.

-- 
Stuart 





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

* Re: "Constraint Error: Range Check" That Does Not Always Occur
  2007-11-22  7:38 ` Damien Carbonne
  2007-11-22  9:12   ` anon
@ 2007-11-22  9:56   ` Stephen Leake
  2007-11-26 18:38   ` Adam Beneschan
  2 siblings, 0 replies; 8+ messages in thread
From: Stephen Leake @ 2007-11-22  9:56 UTC (permalink / raw)


Damien Carbonne <damien.carbonne@free.fr> writes:

> DarthBob88 a �crit :

>> 	package Random_Die is new Ada.Numerics.Discrete_Random (Die);
> Here you instantiate Discrete_Random with Die.
> Change Die to Dice, it should work.

>> 	D4 := Random(G);
> Here, Random should have been instantiated with Dice.

If you had used types, instead of subtypes, for Die and Dice, Ada
would have warned you about this mistake.

-- 
-- Stephe



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

* Re: "Constraint Error: Range Check" That Does Not Always Occur
  2007-11-22  6:42 "Constraint Error: Range Check" That Does Not Always Occur DarthBob88
  2007-11-22  7:38 ` Damien Carbonne
  2007-11-22  9:47 ` Stuart
@ 2007-11-22 14:14 ` Matthew Heaney
  2007-11-23  3:48   ` DarthBob88
  2 siblings, 1 reply; 8+ messages in thread
From: Matthew Heaney @ 2007-11-22 14:14 UTC (permalink / raw)


DarthBob88 <darthbob88@gmail.com> writes:

> procedure test is
> 	subtype Die is Integer range 1..6;
> 	--Declare that anything of type Die may be
> 	--an integer, value between 1 and 6, inclusive, and sod-all else.

That's fine.


> 	subtype Dice is Integer range 3*Die'First .. 3*Die'Last;
> 	--Declares that Dice may be anything from 3 to 18.

But you sum 4 dice; that's a min of 4 * 1 and a max of 4 * 6 -- why did you
make your Dice subtype too small?


> 	package Random_Die is new Ada.Numerics.Discrete_Random (Die);
> 	use Random_Die;
> 	--Random generator; produces an integer between 1 and 6

Right.


> 	sum : Integer := 0;
> 	G: Generator;
>      	D1 : Dice;
> 	D2 : Dice;
> 	D3 : Dice;
> 	D4 : Dice;
> 	--Declare four Dice

--or:   D1, D2, D3, D4 : Die;  -- DICE IS THE WRONG SUBTYPE
--      Sum : Dice;  -- subtype should be 4 .. 24

> begin
> 	Reset(G);
> 	D1 := Random(G);
> 	D2 := Random(G);
> 	D3 := Random(G);
> 	D4 := Random(G);
> 	--I Haven't found the error just yet, but a Constraint_Error
> 	--Occasionally comes up for the previous 4 lines
> 	--I haven't been able to find it online, but I presume
> 	--It's because the generator comes up with a number outside of [1,6]

The generator will return you a random number between 1 and 6 (inclusive).

Your Die subtype has a minimum bounds of 1, so the random number can (and will)
return values that include the values 1 and 2.  

Your Dice subtype (to which you assign the value returned by the generator) has
a minimum bound of 3.  So when the random number generator returns a value less
then 3, you get C_E during the assignment.

Just change the declarations of Dx to use subtype Die instead of Dice, and then
all is well.


> 	sum := D1 + D2 + D3 + D4;
> 	--Debugging
> 	Ada.Integer_Text_IO.Put(D1);
> 	Ada.Integer_Text_IO.Put(D2);
> 	Ada.Integer_Text_IO.Put(D3);
> 	Ada.Integer_Text_IO.Put(D4);
> 	Ada.Integer_Text_IO.Put(sum);
> end test;
> --compile Ada programs with gnat make <foo>.adb
>
> If it helps any, I'm using the Windows version of GNAT on XP. Thanks
> in advance.





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

* Re: "Constraint Error: Range Check" That Does Not Always Occur
  2007-11-22 14:14 ` Matthew Heaney
@ 2007-11-23  3:48   ` DarthBob88
  0 siblings, 0 replies; 8+ messages in thread
From: DarthBob88 @ 2007-11-23  3:48 UTC (permalink / raw)


On Nov 22, 6:14 am, Matthew Heaney <matthewjhea...@earthlink.net>
wrote:
> DarthBob88 <darthbo...@gmail.com> writes:
> > procedure test is
> >    subtype Die is Integer range 1..6;
> >    --Declare that anything of type Die may be
> >    --an integer, value between 1 and 6, inclusive, and sod-all else.
>
> That's fine.
>
> >    subtype Dice is Integer range 3*Die'First .. 3*Die'Last;
> >    --Declares that Dice may be anything from 3 to 18.
>
> But you sum 4 dice; that's a min of 4 * 1 and a max of 4 * 6 -- why did you
> make your Dice subtype too small?
That would because I cut'n'pasted it from a program to simulate
rolling two dice. As for the 3 and 4 discrepancy, that's just
stupidity on my part. I was trying to simulate the sum of three dice,
chosen from four that were rolled. Hence, that little idiocy.
*facepalm*

> >    package Random_Die is new Ada.Numerics.Discrete_Random (Die);
> >    use Random_Die;
> >    --Random generator; produces an integer between 1 and 6
>
> Right.
>
> >    sum : Integer := 0;
> >    G: Generator;
> >            D1 : Dice;
> >    D2 : Dice;
> >    D3 : Dice;
> >    D4 : Dice;
> >    --Declare four Dice
>
> --or:   D1, D2, D3, D4 : Die;  -- DICE IS THE WRONG SUBTYPE
> --      Sum : Dice;  -- subtype should be 4 .. 24
>
> > begin
> >    Reset(G);
> >    D1 := Random(G);
> >    D2 := Random(G);
> >    D3 := Random(G);
> >    D4 := Random(G);
> >    --I Haven't found the error just yet, but a Constraint_Error
> >    --Occasionally comes up for the previous 4 lines
> >    --I haven't been able to find it online, but I presume
> >    --It's because the generator comes up with a number outside of [1,6]
>
> The generator will return you a random number between 1 and 6 (inclusive).
>
> Your Die subtype has a minimum bounds of 1, so the random number can (and will)
> return values that include the values 1 and 2.
>
> Your Dice subtype (to which you assign the value returned by the generator) has
> a minimum bound of 3.  So when the random number generator returns a value less
> then 3, you get C_E during the assignment.
>
> Just change the declarations of Dx to use subtype Die instead of Dice, and then
> all is well.
Thank you kindly, sir. Ladies and gentlemen, this is why you should
not code at 0-dark-30, because you make idiotic mistakes like this,
instead of thinking through them.
> >    sum := D1 + D2 + D3 + D4;
> >    --Debugging
> >    Ada.Integer_Text_IO.Put(D1);
> >    Ada.Integer_Text_IO.Put(D2);
> >    Ada.Integer_Text_IO.Put(D3);
> >    Ada.Integer_Text_IO.Put(D4);
> >    Ada.Integer_Text_IO.Put(sum);
> > end test;
> > --compile Ada programs with gnat make <foo>.adb
>
> > If it helps any, I'm using the Windows version of GNAT on XP. Thanks
> > in advance.



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

* Re: "Constraint Error: Range Check" That Does Not Always Occur
  2007-11-22  7:38 ` Damien Carbonne
  2007-11-22  9:12   ` anon
  2007-11-22  9:56   ` Stephen Leake
@ 2007-11-26 18:38   ` Adam Beneschan
  2 siblings, 0 replies; 8+ messages in thread
From: Adam Beneschan @ 2007-11-26 18:38 UTC (permalink / raw)


On Nov 21, 11:38 pm, Damien Carbonne <damien.carbo...@free.fr> wrote:
> DarthBob88 a écrit :
>
>
>
>
>
> > I've been trying to write a program that creates four random numbers,
> > to simulate four dice rolling; a D&D simulator, in fact.
> > Unfortunately, I occasionally get "raised CONSTRAINT_ERROR : test.adb:
> > 22 range check failed" (line 22 is one of the lines to generate a
> > random number; it varies between the four). I have been trying to find
> > information on how to prevent this error, but I can't find anything
> > about it. I'm extremely new to this language, as in I only started
> > last week new, so any help would be greatly appreciated. Here follows
> > my code, warts and all.
>
> > with Ada.Text_IO, Ada.Integer_Text_IO;
> > with Ada.Numerics.Discrete_Random;
>
> > procedure test is
> >    subtype Die is Integer range 1..6;
> >    --Declare that anything of type Die may be
> >    --an integer, value between 1 and 6, inclusive, and sod-all else.
> >    subtype Dice is Integer range 3*Die'First .. 3*Die'Last;
> >    --Declares that Dice may be anything from 3 to 18.
> >    package Random_Die is new Ada.Numerics.Discrete_Random (Die);
>
> Here you instantiate Discrete_Random with Die.
> Change Die to Dice, it should work.>    use Random_Die;
> >    --Random generator; produces an integer between 1 and 6
> >    sum : Integer := 0;
> >    G: Generator;
> >            D1 : Dice;
> >    D2 : Dice;
> >    D3 : Dice;
> >    D4 : Dice;
> >    --Declare four Dice
> > begin
> >    Reset(G);
> >    D1 := Random(G);
> >    D2 := Random(G);
> >    D3 := Random(G);
> >    D4 := Random(G);
>
> Here, Random should have been instantiated with Dice.

No, that won't give the desired results.  If the intent is to simulate
rolling three dice and adding them up, you will not get what you want
by generating a random number from 3 to 18, because that makes every
possible integer from 3 through 18 equally likely, which of course is
not true.  You do want to instantiate Random with Die to simulate each
die roll.  The error here is that I think D1, D2, and D3 should have
been declared as having subtype "Die" instead of "Dice", since D1, D2,
and D3 each represent the result of one die roll, not a sum of three
dice.  (I'm confused about whether there are three dice or four, but
the point is the same either way.)

                         -- Adam



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

end of thread, other threads:[~2007-11-26 18:38 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2007-11-22  6:42 "Constraint Error: Range Check" That Does Not Always Occur DarthBob88
2007-11-22  7:38 ` Damien Carbonne
2007-11-22  9:12   ` anon
2007-11-22  9:56   ` Stephen Leake
2007-11-26 18:38   ` Adam Beneschan
2007-11-22  9:47 ` Stuart
2007-11-22 14:14 ` Matthew Heaney
2007-11-23  3:48   ` DarthBob88

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