jeudi 12 mai 2016

Creating new random vs using same random

I'm trying to distribute a system and I need to use nextGaussian() from the Random Java class. The only way I found to break the data dependency a unique seed creates was using multiple seeds, thus, creating multiple randoms.

Let's forget about the context of my problem, I just want to know how well normalized is using multiple Random instances compared to using only one instance. In other words... How random is this genNew compared to genSame?

The code:

public double[] genNew(int lim, long seed)
{
    double[] rand = new double[lim];
    for(int i = 0; i < lim; i++)
    {
        //A random for every iteration.
        Random r = new Random(i*seed);
        rand[i] = r.nextGaussian();
    }
    return rand;
}

public double[] genSame(int lim, long seed)
{
    double[] rand = new double[lim];
    //A random for all iterations
    Random r = new Random(seed);
    for(int i = 0; i < lim; i++)
        rand[i] = r.nextGaussian();
    return rand;
}

The results I get are very different when I apply both arrays to my code. I just can't explain why.

EDIT: I know this won't generate the same arrays, it is just that when using a big ammount of normalized randoms to calculate a number, both numbers should be close (because of the normalization), but they aren't.




Aucun commentaire:

Enregistrer un commentaire