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=-0.9 required=5.0 tests=BAYES_00,FROM_NUMERIC_TLD autolearn=no 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!news2.google.com!news.glorb.com!news-peer-lilac.gradwell.net!not-for-mail From: "Stuart" Newsgroups: comp.lang.ada References: <86ddf1f4-c7be-4729-8480-bae618b6d296@a39g2000pre.googlegroups.com> Subject: Re: "Constraint Error: Range Check" That Does Not Always Occur Date: Thu, 22 Nov 2007 09:47:47 -0000 X-Priority: 3 X-MSMail-Priority: Normal X-Newsreader: Microsoft Outlook Express 6.00.2900.3138 X-RFC2646: Format=Flowed; Original X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2900.3198 Message-ID: <47454c83$1_1@glkas0286.greenlnk.net> X-Original-NNTP-Posting-Host: glkas0286.greenlnk.net NNTP-Posting-Host: 20.133.0.1 X-Trace: 1195724875 news.gradwell.net 516 dnews/20.133.0.1:18083 X-Complaints-To: news-abuse@gradwell.net Xref: g2news1.google.com comp.lang.ada:18561 Date: 2007-11-22T09:47:47+00:00 List-Id: "DarthBob88" 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