Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

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":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others scrutinizing the Monastery: (7)
As of 2024-04-24 10:02 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found