George Johnston
George Johnston

Reputation: 32258

C#/Java Number Randomization

Is it possible, from .NET, to mimic the exact randomization that Java uses? I have a seed, and I would like to be able to recieve the same results in both C# and Java when creating a random number.

Upvotes: 10

Views: 3094

Answers (6)

finnw
finnw

Reputation: 48619

You don't need to read the source code. The formula is a one-liner and is given in the documentation for java.util.Random.

Here's a partial translation:

[Serializable]
public class Random
{
    public Random(UInt64 seed)
    {
        this.seed = (seed ^ 0x5DEECE66DUL) & ((1UL << 48) - 1);
    }

    public int NextInt(int n)
    {
        if (n <= 0) throw new ArgumentException("n must be positive");

        if ((n & -n) == n)  // i.e., n is a power of 2
            return (int)((n * (long)Next(31)) >> 31);

        long bits, val;
        do
        {
            bits = Next(31);
            val = bits % (UInt32) n;
        }
        while (bits - val + (n - 1) < 0);

        return (int) val;
    }

    protected UInt32 Next(int bits)
    {
        seed = (seed * 0x5DEECE66DL + 0xBL) & ((1L << 48) - 1);

        return (UInt32)(seed >> (48 - bits));
    }

    private UInt64 seed;
}

Example:

Random rnd = new Random(42);
Console.WriteLine(rnd.NextInt(10));
Console.WriteLine(rnd.NextInt(20));
Console.WriteLine(rnd.NextInt(30));
Console.WriteLine(rnd.NextInt(40));
Console.WriteLine(rnd.NextInt(50));

Output on both platforms is 0, 3, 18, 4, 20.

Upvotes: 8

Mehrdad Afshari
Mehrdad Afshari

Reputation: 421988

If you have the source code of the java.util.Random class for your Java implementation, you can easily port it to .NET.

If you require both applications (Java and .NET) to use a certain random number generator, you'd better implement one in both platforms and use it instead, as the system provided version might change its behavior as a result of an update.(Looks like the Java specification precisely describes the behavior of its PRNG.)

Upvotes: 5

jk.
jk.

Reputation: 14004

Another option might be to write your random numbers out to a file once from one platform and then just load your random numbers for both platforms from that file, or you could load them from a service such as random.org

Upvotes: -2

Jonas Elfstr&#246;m
Jonas Elfstr&#246;m

Reputation: 31428

If you don't need a cryptographically secure pseudorandom number generator then I would go for the Mersenne twister. You can find source code for C# here and Java here.

Upvotes: 2

Maybe it would make sense to implement your own simple pseudo-random number generator? That way you have complete control and can garauntee the same seed gives the same results in both environments. Probably a bit more work than porting one to the other though.

Upvotes: 0

Steve B.
Steve B.

Reputation: 57284

Well, you can look in the source code for Random.java and copy the algorithm, constants, etc.etc, but Random uses System.nanoTime in its constructor so you won't get the same results.

From java.util.Random

public Random() { this(++seedUniquifier + System.nanoTime()); }

I wouldn't be at all surprised if the source in C# would show you something similar.

Edit: Disregard, as has been pointed out, the constructor that takes an input seed never accesses time.

Upvotes: 0

Related Questions