Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Re: how to get a 64bit random number with rand() ?

by bliako (Monsignor)
on Mar 22, 2018 at 14:55 UTC ( [id://1211529]=note: print w/replies, xml ) Need Help??


in reply to how to get a 64bit random number with rand() ?

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

Replies are listed 'Best First'.
Re^2: how to get a 64bit random number with rand() ?
by QM (Parson) on Mar 22, 2018 at 15:34 UTC
    I always wanted to ask Einsten to clarify whether "dice" in his famous "God does not play dice" is plurar or singular.
    God will have an infinite-sided die (a sphere), and can therefore get any real number with only 1 throw.

    -QM
    --
    Quantum Mechanics: The dreams stuff is made of

      the distribution the number is drawn from is what counts. Uniform then unless the sphere is not honest.

      It is obvious that Einstein thought God to be singular (as per LanX) for the simple reason that a multitude of Gods would probably combine their random numbers which would result in a Gaussian again...

      Polytheism results in an extremely biased (and predictable) Universe.

      bliako

        Polytheism decreases entropy.

        p.s. I am not sure these little, irrelevant, fun in my opinion, snippets are forbidden by the rules or frown upon my fellow Monks. If yes, please someone alert me.

        bliako
Re^2: how to get a 64bit random number with rand() ?
by LanX (Saint) on Mar 22, 2018 at 16:04 UTC
    AFAIK he said "Gott würfelt nicht!" *

    "würfeln" is a verb for playing with dice without numerus for the object. ("God is not dicing!"° if you want)

    His English was so rudimentary, that his assistants in Princeton had to be fluent in German.

    But I'm sure he supposed God to be singular...

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

    *) according to wikipedia from 1926

    °) "to dice" exists at least in Shakespearian English and Game of Thrones.

Re^2: how to get a 64bit random number with rand() ?
by Anonymous Monk on Mar 22, 2018 at 18:11 UTC

    Now if you *add* two rand() values in order, for example, to get a 64-bit random value
    You have omitted a little bit of detail that was not omitted in BrowserUk's warning: the context was combining two 32-bit values to obtain a 64-bit value.

    Can you please elaborate on how it is possible to ADD two 32-bit numbers — of any origin, not necessarily rand() — and arrive at a value in 64-bit range?

      my point lies somewhere else, but you are correct:

      adding two 32-bit numbers may get you to, at most, a 33-bit number.

      Read that as "add thirty-two 32-bit rands()" (Gaussian distribution) or "multiply two 32-bit rands()" (uniform-product distribution).

      Adding is just one form of combining two values but it's perfect to demonstrate the caveats of "combining" rand()s and that not all forms of "combining" rand()s lead to the desirable effect.

      bliako

        Read that as "add thirty-two 32-bit rands()"
        You sure about that?

        The issue of entropy distilling is a concern for random number generators. Various sources of randomness are mixed into the pool and typically some one-way hashing applied on the output.

        Unless if you're trying to write a PRNG yourself, you don't want to mix anything. The generator is essentially a stream (and implementing such via IO layers is actually very simple.) From a stream, one just consumes characters.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others surveying the Monastery: (3)
As of 2024-04-19 22:01 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found