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.3 required=5.0 tests=BAYES_00, REPLYTO_WITHOUT_TO_CC autolearn=no autolearn_force=no version=3.4.4 X-Google-Thread: 103376,8147387fe25d4e2a X-Google-NewGroupId: yes X-Google-Attributes: gida07f3367d7,domainid0,public,usenet X-Google-Language: ENGLISH,ASCII-7-bit Path: g2news1.google.com!news4.google.com!feeder2.cambriumusenet.nl!feed.tweaknews.nl!192.87.166.28.MISMATCH!tudelft.nl!txtfeed1.tudelft.nl!newsfeed10.multikabel.net!multikabel.net!newsfeed20.multikabel.net!border3.nntp.ams.giganews.com!Xl.tags.giganews.com!border1.nntp.ams.giganews.com!nntp.giganews.com!local2.nntp.ams.giganews.com!nntp.bt.com!news.bt.com.POSTED!not-for-mail NNTP-Posting-Date: Thu, 30 Dec 2010 05:49:41 -0600 From: Brian Drummond Newsgroups: comp.lang.ada Subject: Re: Random number generation Date: Thu, 30 Dec 2010 11:51:16 +0000 Reply-To: brian@shapes.demon.co.uk Message-ID: References: <864o9vbkwz.fsf@gareth.avalon.lan> X-Newsreader: Forte Agent 1.7/32.534 MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit X-Usenet-Provider: http://www.giganews.com X-AuthenticatedUsername: NoAuthUser X-Trace: sv3-G1ZJVOphYydqMXaJcY5eBjEO3QQpFgVItdJX9/5u6tIGX3o43EYxgCc8lEpxlewNen206jvhEUYXL9h!tiPL5AmHSl890IpMNRZx0byDuDCa6oyu+Irok/mr7OfUb+n3PPw20TlxXTvkyD1vFtvcY1Jk2H3j!ASgn X-Complaints-To: abuse@btinternet.com X-DMCA-Complaints-To: abuse@btinternet.com X-Abuse-and-DMCA-Info: Please be sure to forward a copy of ALL headers X-Abuse-and-DMCA-Info: Otherwise we will be unable to process your complaint properly X-Postfilter: 1.3.40 X-Original-Bytes: 2919 Xref: g2news1.google.com comp.lang.ada:16249 Date: 2010-12-30T11:51:16+00:00 List-Id: On Thu, 30 Dec 2010 11:43:40 +0100, Mart van de Wege wrote: >Beginner's question: I'm trying to implement a function that rolls a >number of dice and then adds a modifier. Somehow, it produces the same >number every time. I can't see where I'm going wrong. > >Here's the proof of concept code: > >with Ada.Numerics.Discrete_Random,Ada.Integer_Text_IO; >use Ada.Integer_Text_IO; > >procedure Rolltest is > function Roll ( Number : in Positive; ... > package Die is new Ada.Numerics.Discrete_Random( Die_Size ); ... > begin > Die.Reset(G); ... > end Roll; >begin > for I in 1..10 loop > Put(Roll( Number => 3, Size => 6 )); > end loop; >end Rolltest; > >Anyone care to at least point me to some documentation that explains >what I'm doing wrong? Check the documentation for Ada.Numerics.Discrete_Random or at least its specification ( .ads file) but I think you'll find you need to reset the Die exactly once, rather than every call of the Roll function. As I understand, you are resetting the seed each time, so you get the same results! The shortest way to resolve this is to make Die a global variable and reset it at the start of the main program - in the context of a one-page beginner's program it's a reasonable thing to do. We're all taught "Global Variables are BAD" with good reason, so - once you have resolved the immediate problem - it might be a good time to learn a bit about packages, to hide the Die and expose only what you need to operate on it. Essentially, within the package, Die can safely become a "global" variable, but invisible and inaccessible outside the package, so that it appears more like a static variable in a C function (holds the current random seed value between calls). - Brian