| 1 |
#!/usr/bin/perl -T |
| 2 |
|
| 3 |
# examples/distribution.pl |
| 4 |
# Check the uniformity of the distribution generated by ISAAC by |
| 5 |
# charting it. |
| 6 |
|
| 7 |
use strict; |
| 8 |
use warnings; |
| 9 |
|
| 10 |
use Math::Random::ISAAC::XS; |
| 11 |
use Chart::Bars; |
| 12 |
|
| 13 |
=head1 NAME |
| 14 |
|
| 15 |
distribution.pl - Show the distribution of numbers generated |
| 16 |
|
| 17 |
=head1 VERSION |
| 18 |
|
| 19 |
Version 1.0 |
| 20 |
|
| 21 |
=cut |
| 22 |
|
| 23 |
use version; our $VERSION = qv('1.0'); |
| 24 |
|
| 25 |
=head1 SYNOPSIS |
| 26 |
|
| 27 |
Usage: distribution.pl |
| 28 |
|
| 29 |
This script plots the distribution of data generated by ISAAC. It's a pretty |
| 30 |
simple module but might eventually make something neat, as the data is being |
| 31 |
plotted randomly. However it's unlikely to do so, since ISAAC is uniformly |
| 32 |
distributed. |
| 33 |
|
| 34 |
It outputs a file named C<chart.png> in the current working directory. |
| 35 |
Currently this is hardcoded, but this should probably change in the future. |
| 36 |
|
| 37 |
=head1 DESCRIPTION |
| 38 |
|
| 39 |
This module uses 10 million numbers for graphing, so hopefully it removes |
| 40 |
any potential slight nonuniformities caused by the seed. |
| 41 |
|
| 42 |
Currently it just uses an instance seeded once with the time. This isn't |
| 43 |
quite as good as using an ISAAC instance to initialize another one, since |
| 44 |
it only begins with one seed value and must do enough mixing to start |
| 45 |
getting some random-looking data. |
| 46 |
|
| 47 |
Most likely though, the data will just be a uniform-looking graph. If you |
| 48 |
find otherwise then you've found a bias with ISAAC, and that's a potential |
| 49 |
security bug. In that case, you should contact Bob Jenkins, author of the |
| 50 |
algorithm. |
| 51 |
|
| 52 |
=cut |
| 53 |
|
| 54 |
my $no_bins = 50; |
| 55 |
my $size = 1 / $no_bins; |
| 56 |
my $count = 10_000_000; |
| 57 |
my $rng = Math::Random::ISAAC::XS->new(time); |
| 58 |
my @bins; |
| 59 |
|
| 60 |
print "Generating some data (this might take a while)...\n"; |
| 61 |
for (1..$count) { |
| 62 |
my $rand = $rng->rand(); |
| 63 |
$bins[$rand*$no_bins]++; |
| 64 |
} |
| 65 |
|
| 66 |
print "Creating a chart...\n"; |
| 67 |
my $chart = Chart::Bars->new(640, 480); |
| 68 |
$chart->set( |
| 69 |
title => 'ISAAC Data Distribution', |
| 70 |
legend => 'none', |
| 71 |
x_label => 'Quantile', |
| 72 |
y_label => 'Number of Items', |
| 73 |
spaced_bars => 0, |
| 74 |
min_val => 0, |
| 75 |
max_val => $count / ($no_bins-1), |
| 76 |
max_y_ticks => 10, |
| 77 |
); |
| 78 |
|
| 79 |
$chart->add_dataset (1..$no_bins); |
| 80 |
$chart->add_dataset (@bins); |
| 81 |
|
| 82 |
$chart->png('chart.png'); |
| 83 |
|
| 84 |
=head1 AUTHOR |
| 85 |
|
| 86 |
Jonathan Yu E<lt>[email protected]<gt> |
| 87 |
|
| 88 |
=head1 SUPPORT |
| 89 |
|
| 90 |
For support details, please look at C<perldoc Math::Random::ISAAC> and |
| 91 |
use the corresponding support methods. |
| 92 |
|
| 93 |
=head1 LICENSE |
| 94 |
|
| 95 |
This has the same copyright and licensing terms as L<Math::Random::ISAAC::XS>. |
| 96 |
|
| 97 |
=head1 SEE ALSO |
| 98 |
|
| 99 |
L<Math::Random::ISAAC>, |
| 100 |
L<Math::Random::ISAAC::PP>, |
| 101 |
L<Math::Random::ISAAC::XS>, |
| 102 |
|
| 103 |
=cut |