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