Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

MZT random number generator

by js29a (Chaplain)
on Jun 20, 2005 at 19:41 UTC ( [id://468468]=sourcecode: print w/replies, xml ) Need Help??
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;
Replies are listed 'Best First'.
Re: MZT random number generator
by merlyn (Sage) on Jun 20, 2005 at 21:14 UTC
      If I read the code correctly, this is a Perl implementation of this, of which is claimed:
      THIS IS THE BEST KNOWN RANDOM NUMBER GENERATOR AVAILABLE.
      ...
      It passes ALL of the tests for random number generators and has a period of 2^144, is completely portable (gives bit identical results on all machines with at least 24-bit mantissas in the floating point representation).
      so if you are looking for a deterministic, long-period pseudo-random number generator, perhaps this is it.

      - j

Re: MZT random number generator
by Anonymous Monk on Jun 19, 2006 at 14:31 UTC
    Hi, I was wondering if you could give a slightly more explained version of this code, it is something i really want to implement but am struggling with maths and code (V. new to perl). Thanks in advance. p.s. I can find my log in details :( just got them too

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others learning in the Monastery: (4)
As of 2024-03-28 15:30 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found