Java random always returns the same number when I set the seed?

You need to share the Random() instance across the whole class:

public class Numbers {
    Random randnum;

    public Numbers() {
        randnum = new Random();
        randnum.setSeed(123456789);
    }

    public int random(int i){
        return randnum.nextInt(i);
    }
}

Leave a Comment