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 Path: g2news1.google.com!postnews.google.com!e23g2000prf.googlegroups.com!not-for-mail From: Adam Beneschan Newsgroups: comp.lang.ada Subject: Re: "Constraint Error: Range Check" That Does Not Always Occur Date: Mon, 26 Nov 2007 10:38:32 -0800 (PST) Organization: http://groups.google.com Message-ID: <3efef509-4654-4b7a-87b2-977833f714cd@e23g2000prf.googlegroups.com> References: <86ddf1f4-c7be-4729-8480-bae618b6d296@a39g2000pre.googlegroups.com> <474531FD.5090401@free.fr> NNTP-Posting-Host: 66.126.103.122 Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable X-Trace: posting.google.com 1196102312 24655 127.0.0.1 (26 Nov 2007 18:38:32 GMT) X-Complaints-To: groups-abuse@google.com NNTP-Posting-Date: Mon, 26 Nov 2007 18:38:32 +0000 (UTC) Complaints-To: groups-abuse@google.com Injection-Info: e23g2000prf.googlegroups.com; posting-host=66.126.103.122; posting-account=duW0ogkAAABjRdnxgLGXDfna0Gc6XqmQ User-Agent: G2/1.0 X-HTTP-UserAgent: Mozilla/5.0 (X11; U; Linux x86_64; en-US; rv:1.7.12) Gecko/20050922 Fedora/1.7.12-1.3.1,gzip(gfe),gzip(gfe) Content-Disposition: inline Xref: g2news1.google.com comp.lang.ada:18635 Date: 2007-11-26T10:38:32-08:00 List-Id: On Nov 21, 11:38 pm, Damien Carbonne wrote: > DarthBob88 a =E9crit : > > > > > > > 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 :=3D 0; > > G: Generator; > > D1 : Dice; > > D2 : Dice; > > D3 : Dice; > > D4 : Dice; > > --Declare four Dice > > begin > > Reset(G); > > D1 :=3D Random(G); > > D2 :=3D Random(G); > > D3 :=3D Random(G); > > D4 :=3D 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