http://www.perlmonks.org?node_id=468468
Category: Miscellaneous
Author/Contact Info jacek s., js29a@ceti.pl
Description: Tiny module for high quality random number generation. Uses MZT method. The only thing you need to do is call r_univ() sub to get high-quality random number from 0.0 to 1.0.

# MZT random number generator
#
# distribution: uniform
# use r_univ() to get number from 0.0 to 1.0

package r_univ;

use strict;
use warnings;
use Exporter;

our @ISA=qw/Exporter/;

our @EXPORT=qw/r_univ/;

my @uu;

my ($cc,$cd,$cm)=(362436.0/16777216.0,
                  7654321.0/16777216.0,
                  16777213.0/16777216.0);

my ($ip,$jp)=(97,33);

sub r_univ_init_rep($$$$){
  my ($wi,$wj,$wk,$wl)=@_;
  my ($m,$t);

  foreach my $ii (0..96){
    my ($s,$t)=(0,0.5);
    foreach my $jj (1..24){
      $m=((($wi*$wj)%179)*$wk)%179;
      $wi=$wj;
      $wj=$wk;
      $wk=$m;
      $wl=(53*$wl+1)%169;
      $s+=$t if (($wl*$m)&63)>0x1f;
      $t*=0.5;
      $uu[$ii]=$s;
    }
  }
}

sub r_univ_init(){
  srand(time());
  my ($a,$b,$c,$d);
  do{
    $a=1+rand()*178;
    $b=1+rand()*178;
    $c=1+rand()*178;
    $d=rand()*167;
  }while($a+$b+$c <= 3);
  r_univ_init_rep($a,$b,$c,$d);
}

sub r_univ(){
  my $aux;
  $aux=$uu[$ip-1]-$uu[$jp-1];
  ++$aux if($aux<0.0);
  $uu[$ip-1]=$aux;
  --$ip;
  $ip=97 unless $ip;
  --$jp;
  $jp=97 unless $jp;
  $cc-=$cd;
  $cc+=$cm if $cc<0.0;
  $aux-=$cc;
  ++$aux if $aux<0.0;
  return $aux;
}

r_univ_init();

1;