Recently, I was reading about random.org again.  It is an online random number generating service that uses atmospheric noise gathered from radios tuned between stations as a source of randomness.  It has been running more or less continuously for about ten years.   On their analysis page there is a nice pair of bitmaps (scroll down past the Dilbert comic) that contrast their random bits with those from one version of the PHP rand() function. Basically this demonstrates how easy it is to create a pseudo-random number generator that is periodic with too small of a period.

I decided to take a look at Maple's random number generator in comparison.

First, here is a 256px square generated by random.org's bitmap generator:

Of course, Maple has a bunch of random number generators. Since Maple 10, rand() has called the MersenneTwister in the RandomTools package. But there are actually three other Random Generators as well:

  • Linear and Quadratic Congruence are simple Pseudorandom generators - before Maple 10 rand() was more or less equivalent to LinearCongruence
  • Blum Blum Shub is a type quadratic congruence, but designed for cryptographic applications

Here is how to create one of each:

Seed := 749174032174023174398217651252100347882175301621678436520;
B := RandomTools[BlumBlumShub][NewBitGenerator](Seed, numbits=1);
L := RandomTools[LinearCongruence][NewGenerator](range=0..1);
Q := RandomTools[QuadraticCongruence][NewGenerator](range=0..1);
M := RandomTools[MersenneTwister][NewGenerator](range=0..1);
And now make a random image from each one:
for r in [B, L, Q, M] do
cat(M,r) := LinearAlgebra[RandomMatrix](256,256, generator=r,
outputoptions=[order=C_order, datatype=float[8]]);
end do:

And you can see that the random generators look pretty good:

ImageTools[View]([ML,MQ],[MM,MB]);

Looking back through older versions of Maple, rand() has always been pretty good with respect to this visual test. One had to be careful using the fast pseudo-random number generator in RandomMatrix way back in Maple 6 however, because you could get this:

# Maple 6 did not have ImageTools, so here I create the matrix
# by calling maple6 using ssystem, then I read it into Maple 12
# This will probably only work in Linux
m6cmd := cat("echo \"LinearAlgebra[RandomMatrix](256, 256,
generator=0..1, outputoptions=[order=C_order, datatype=float[8]]):
save M6, \\\"/tmp/M6.m\\\";\" | maple6 -s -q");
ssystem(m6cmd);
read("/tmp/M6.m");
ImageTools[View](M6);


The fast random number generator used by RandomMatrix was fixed in Maple 8 however, and now it does not have any period effects.

Here is an example of how to build a bad pseudo-random number generator.  This is a linear congruence with a non-prime modulus:

LCState := 1;
r := proc()
global LCState;
LCState := irem(89853*LCState, 50027);
irem(LCState, 2);
end proc;

Mr := LinearAlgebra[RandomMatrix](256,256, generator=r,
outputoptions=[order=C_order, datatype=float[8]]);

# It _almost_ looks okay
ImageTools[View](Mr);

edited 7 Jun 2010 to replace images


Please Wait...