mercredi 19 octobre 2016

Under what circumstances that you would want to have a different seed for you random number generator?

I am wondering under what circumstances that you would want to have a different seed for you random number generator. I see code somewhere that people create a Random object every time when needed, and also see people sometimes would have it as an instance variable that uses a single seed when the object is constructed. For example

// same seed
class A {
    private Random random;
    public A() { random = new Random(); }
    public int nextInt() { return random.nextInt(10000); }
    public double nextDouble() { return random.nextDouble(); }
}

versus

// different seed is used every time when you call the method
class B {
    public int nextInt() { return new Random().nextInt(10000); }
    public double nextDouble() { return new Random().nextDouble(); }
}

Please ignore this design for these two wrapper classes, just trying to show the difference.




Aucun commentaire:

Enregistrer un commentaire