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-Thread: 103376,2ac1e7c090dd0c4a X-Google-Attributes: gid103376,public,usenet X-Google-Language: ENGLISH,ASCII-7-bit Path: g2news1.google.com!news1.google.com!border1.nntp.dca.giganews.com!border2.nntp.dca.giganews.com!novia!feeder.news-service.com!newsfeed.freenet.de!border2.nntp.ams.giganews.com!nntp.giganews.com!sn-xt-ams-06!sn-xt-ams-04!sn-ams!sn-feed-ams-03!sn-post-ams-01!sn-post-sjc-01!supernews.com!corp.supernews.com!not-for-mail From: Matthew Heaney Newsgroups: comp.lang.ada Subject: Re: "Constraint Error: Range Check" That Does Not Always Occur Date: Thu, 22 Nov 2007 14:14:04 +0000 Organization: Posted via Supernews, http://www.supernews.com Message-ID: References: <86ddf1f4-c7be-4729-8480-bae618b6d296@a39g2000pre.googlegroups.com> User-Agent: Gnus/5.11 (Gnus v5.11) Emacs/22.1 (windows-nt) Cancel-Lock: sha1:aQQtPP/fljKZgn6A7Za/8jKSaWQ= MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii X-Complaints-To: abuse@supernews.com X-Original-Bytes: 3000 Xref: g2news1.google.com comp.lang.ada:18565 Date: 2007-11-22T14:14:04+00:00 List-Id: DarthBob88 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 .adb > > If it helps any, I'm using the Windows version of GNAT on XP. Thanks > in advance.