G'day Discipulus,

About a decade ago, I wrote dice rolling code using almost exactly the same code as you show here. It was for use with a table-top RPG; it handled die with any number of sides.

Over the years, I added lots of features around the code (mostly ever-more fancy Tk interfaces) but the core dice-rolling code was unchanged.

I've used this code with Perl versions ranging from 5.12 to 5.32. Perl installations have included Perlbrew, Strawberry Perl and ActivePerl. Platforms have included WinXP & Win10 (both +/- Cygwin) and various Mac OS X/macOS versions.

I don't possess any data on checking averages; although, at different times, I have successfully run similar tests to those you show. In real-world usage, I've never noticed any problems, such as persistently getting high or low rolls; I appreciate that's not really any sort of strict or scientific check.

If your intended usage is similar to mine — you only said "a little project just to clean my rusty hands" — then I'd say rand() is sufficient. If your use case differs, then I see a plethora of advice in other posts that might be more appropriate.

"... can I expect the same result on every platform? and for every perl version around the world?"

Given the number of platforms that can run Perl, and the number of Perl versions around, I doubt you'd ever get a completely definitive answer to that question. However, you could ask people to supply the output from the same code and at least get a representative answer. Here's some suggested code (which I think should run on any version of Perl5):

$ perl -e ' my $iters = 100_000; my $runs = 6; for my $sides (qw{1 2 3 4 6 8 10 20 100}) { printf "%-5s", "D$sides:"; for (1 .. $runs) { my $tot = 0; for (1 .. $iters) { $tot += int(rand $sides)+1; } print " ", $tot/$iters; } print "\n"; } '

As a one-liner, for a quick copy-and-paste (although, given use of the [download] link, copying the original might be just as quick):

perl -e 'my $iters = 100_000; my $runs = 6; for my $sides (qw{1 2 3 4 +6 8 10 20 100}) { printf "%-5s", "D$sides:"; for (1 .. $runs) { my $t +ot = 0; for (1 .. $iters) { $tot += int(rand $sides)+1; } print " ", +$tot/$iters; } print "\n"; } '

Here's sample output using my Win10/Cygwin/Perlbrew/Perl 5.32.0:

D1: 1 1 1 1 1 1 D2: 1.49943 1.5049 1.49996 1.49833 1.50001 1.50155 D3: 2.00469 1.99941 1.99753 1.99721 1.99762 2.00254 D4: 2.49589 2.49891 2.49487 2.50727 2.49977 2.49472 D6: 3.49716 3.49463 3.49886 3.50365 3.49936 3.50401 D8: 4.50082 4.51098 4.50972 4.49368 4.50158 4.5112 D10: 5.4949 5.48275 5.50242 5.50131 5.49563 5.51278 D20: 10.47763 10.47848 10.50873 10.48685 10.48474 10.51209 D100: 50.36542 50.49534 50.49152 50.40469 50.53791 50.34646

If you could get a few people to post results for a variety of platforms, installations and Perl versions that they might have available, you may get something approaching a reasonable answer to your "every platform ... every version" question.

And, just in case those three nested loops resulting in 5,400,000 calls to rand() is of concern, the whole run only took about one second on my computer. I have a reasonably high-end rig: YMMV but hopefully not too much; I don't think this will tie up anyone's computer for hours.

— Ken


In reply to Re: is rand random enough to simulate dice rolls? by kcott
in thread is rand random enough to simulate dice rolls? by Discipulus

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.