[Mono-devel-list] Mono's Random class and licensing

Ben Maurer bmaurer at ximian.com
Sat Jul 31 15:13:53 EDT 2004

Hello Marcus,

    Mono had a working Random implementation, but it failed under
certain conditions, you can read the evolution of this here:


Basically, whenever the Random implementation was given the seed 0, it
would always return 0 as the random number. If you read Knuth, you will
find that almost every PRNG has some degenerate seeds that lead to
sequences that are not really random. Many vendors will hardcode a seed
into their programs. They have tested that the seed gives them a
suitable sequence of random numbers. If they put their code on Mono and
it turns out that their seed is degenerate in our algorithm, it is an

On Sat, 2004-07-31 at 00:52, Marcus wrote:
> Mono's Random class appears to be almost identical to that of Rotor's,
> including variable names and an apparent typo in Rotor's source code.

You can get more information on how Random.cs morphed from its initial
version to what we have today through a series of bug fixes.  There
have been at least 4 iterations over Random to get to this point (that
I can tell from cvs annotate), the most important ones are:

      * The variable `ma' has been named `SeedArray' as in Rotor.
        This deviation was not actually present in my original version of
        Random.cs. Gert later made a checkin to do this.  Since Random
        is marked [Serializable], both public *and* private variables
        must have the same name. This is the only way to allow
        serialization compatibility between Microsoft and Mono.

      * The constant for inextp is 21 not 31.


        In order to generate the same random numbers as Microsoft, we
        need to use the same constant.
        When I wrote this code, I did the following process:
             1. I did some scouting to see what the common RNG
                algorithms are in the field. Really, there are just a
                few variations on a theme.  
             2. At this point, I implemented the algorithm.
             3. I then found that my RNG did not give the same numbers
                as Microsoft's RNG. At this point, I opened Rotor's
                random.cs, and compared it to the other versions. It
                appears that they had a typo, the use of 21 rather than
                31. Making this change, my numbers were much better.

Even today, we know that our implementation is slightly different from
Microsoft, this is tracked on the CVS history:

        * Random.cs: Corrected random value when Next is called with a
        negative value. Testing indictae that our results aren't exactly the
        same as MS, we have a +/- 1 (probably rounding errors due to
        different implementation).

And it is estimated that 1% of the time we are still generating
different sequences.  

> Even if Mono's source was not taken directly from Rotor's, there is a problem: 
> the code from Numerical Recipes does not permit source redistribution:
> http://www.numerical-recipes.com/com/info-permissions.html

I had not noticed this. We are going to to contact the authors of
NRinC and ask for necessary permission.  If  necessary, the algorithm
can be written from one of the other available references.

-- Ben

More information about the Mono-devel-list mailing list