Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Re: is rand random enough to simulate dice rolls?

by haukex (Bishop)
on Jan 03, 2021 at 15:16 UTC ( #11126207=note: print w/replies, xml ) Need Help??


in reply to is rand random enough to simulate dice rolls?

But the case is I want to put this inside a .t file of a module

If this is for distribution to CPAN, then personally I would try and stay away from anything random - there are lots of different systems out there, and why risk literally random test failures? Instead, I'd ask what it is you're actually testing, because I personally wouldn't see the need for testing that Perl's rand is working correctly, that's the job of the Perl test suite. If you're testing that your code works properly given various inputs, then you can test that by mocking rand - or even write your code so that your PRNG is wrapped in a function, so if you've got a user who wants different sources of randomness, they can switch it out.

Replies are listed 'Best First'.
Re^2: is rand random enough to simulate dice rolls?
by talexb (Canon) on Jan 03, 2021 at 15:50 UTC

    Yup. You'll get an average of 3.5 after a large number of rolls (for some definition of 'large') .. but it's possible that you won't meet the (3.4,3.6) range based on a string of high or low runs in the random number generator, and your test will fail. At that point the user will go .. "Huh?" I'm not sure what their next step might be -- if they're familiar with module installation, they might try a make test, and see that it passes the next time, and continue on to do the install. And some people might ditch the installation completely.

    I'd recommend you change that to a non-critical test.

    Alex / talexb / Toronto

    Thanks PJ. We owe you so much. Groklaw -- RIP -- 2003 to 2013.

      > but it's possible that you won't meet the (3.4,3.6) range based on a string of high or low runs in the random number generator, and your test will fail.

      But that's exactly what the definition of a PRNG guarantees.

      There *exists* a large number, such that the range is met in the run.

      Problematic is only the suggested number 10000, but my gut feeling says it's already big enough.

      I'd say if the OP wants to be sure of that, then he should include it in his test-suite.

      And if it's not critical enough to make the installation fail, he can still use it to emit a warning to the user.

      Cheers Rolf
      (addicted to the Perl Programming Language :)
      Wikisyntax for the Monastery

Re^2: is rand random enough to simulate dice rolls?
by stevieb (Canon) on Jan 03, 2021 at 15:35 UTC
    "If this is for distribution to CPAN, then personally I would try and stay away from anything random - there are lots of different systems out there, and why risk literally random test failures?"

    I wholeheartedly agree with this. Literal random failures.

    The primary reason isn't because a user might run into random failures, because all they have to do is re-run the installer and it'll likely pass the next time, but I know for fact that the maintainers of some of the Tester platforms do spend time investigating why distributions fail, then spend more time contacting the author to inform them as to what's broken and oftentimes spend yet even more time sorting out and offering solutions on the fix.

    I've been the recipient of dozens of said kind emails over the years. I would cringe to think I'd wasted their time after I uploaded tests that would be known to false-negative fail on me.

Re^2: is rand random enough to simulate dice rolls?
by LanX (Sage) on Jan 03, 2021 at 16:38 UTC
    > there are lots of different systems out there, and why risk literally random test failures?

    not random test failures, but testing the implementation.

    afoken linked to an article stating that from 5.20 on Perl brings it's own PRNG doesn't rely on the OS / C-Compiler anymore.

    This also means there are "lots of different systems" out there which are less reliable.

    And if this requirement is crucial for his app, he should abort installation and request another Perl version.

    Cheers Rolf
    (addicted to the Perl Programming Language :)
    Wikisyntax for the Monastery

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://11126207]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (5)
As of 2021-10-28 11:43 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    My first memorable Perl project was:







    Results (96 votes). Check out past polls.

    Notices?