comp.lang.ada
 help / color / mirror / Atom feed
From: anon@anon.org (anon)
Subject: Re: "Constraint Error: Range Check" That Does Not Always Occur
Date: Thu, 22 Nov 2007 09:12:46 GMT
Date: 2007-11-22T09:12:46+00:00	[thread overview]
Message-ID: <iKb1j.131769$kj1.29906@bgtnsc04-news.ops.worldnet.att.net> (raw)
In-Reply-To: 474531FD.5090401@free.fr

[-- 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.




  reply	other threads:[~2007-11-22  9:12 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
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 [this message]
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
replies disabled

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