Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

comment on

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

Can't agree more with BrowserUk.

Whatever you do always verify the statistical distribution of your method for producing random numbers. In order to do that you need to produce a large bunch of these random numbers and plot a histogram of their values or, much more accurate, run a test for verifying the distribution. Here is a test for a discrete distribution, e.g. dice numbers: https://stackoverflow.com/questions/21204733/a-better-chi-square-test-for-perl

Perl's rand() will, in theory, produce random numbers drawn from a uniform distribution: all values from 0 to 1 have equal probability to appear. The histogram of such a distribution is more-or-less a flat line (the equal probabilities). Provided that you draw *a lot* of random numbers you will be approximating that.

Now if you *add* two rand() values in order, for example, to get a 64-bit random value you will find that the histogram of the numbers you get is not a flat line anymore but resembling more of a bell-curve, the trademark of a Gaussian (aka Normal) distribution. I would love to play poker against a machine which uses this method ...

Here is some code to demonstrate this:
#!/usr/bin/env perl use strict; use warnings; use Statistics::Histogram; my $num_bins = 50; my $use_linear_axes = 1; srand(1234); ### Produce a lot of random numbers: # 1) expecting a flat line (uniform distribution): my @x = map { rand() } 1..1000000; # 2) sum of two rand() to get larger random number # - I am getting larger numbers and I am expecting a flat line. # - you wanna bet? my @y = map { rand()+rand() } 1..1000000; # Now draw the histograms of obtained random numbers: # 1) perl's rand is quite uniform, I see a flat line: print Statistics::Histogram::get_histogram(\@x, $num_bins, $use_linear +_axes); # 2) !!ouch - that hurt !! print Statistics::Histogram::get_histogram(\@y, $num_bins, $use_linear +_axes);

Bottomline: I always wanted to ask Einsten to clarify whether "dice" in his famous "God does not play dice" is plurar or singular. I guess I will never know although maybe 60 years ago dice did have the singular of die. If God plays with a single dice his or her distributions will be Uniform (the flat lines). However, if God plays with two dice (and sums up the two rands) his/her distributions will be Gaussian (the bell shape) and that makes a lot of difference for the Universe.

bliako

In reply to Re: how to get a 64bit random number with rand() ? by bliako
in thread how to get a 64bit random number with rand() ? by iglake

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 contemplating the Monastery: (3)
As of 2024-04-16 21:32 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found