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

I require help with a random number generator I am creating. My code is as follows (inside a class called numbers):

public int random(int i){
    Random randnum = new Random();
    randnum.setSeed(123456789);
    return randnum.nextInt(i);
}

When I call this method from another class (in order to generate a random number), it always returns the same number. For example if I were to do:

System.out.println(numbers.random(10));
System.out.print(numbers.random(10));

it always prints the same number e.g. 5 5. What do I have to do so that it prints two different numbers e.g. 5 8

It is mandatory that I set the seed.

Thanks


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);
    }
}

If you always set the seed, you will always get the same answer. That is what setting the seed does.


There are two issues causing what you see. The first is that the code sets a seed value for a Random instance. The second is that the instance method "random" instances a new Random object and then immediately sets its seed with the same seed every time. The combination of these two guarantees that, for the same value of i, the method "random" will always return the same value and it will always be the first in the sequence that the seed always generates.

Assuming setting the seed is mandatory, to get the next value in the sequence instead of the same first value of the sequence every time, the randnum instance of Random can't have its seed set every time just before its next method gets called. To fix that, move the randnum local variable instance of Random from the scope of the random instance method to the class scope. Second, set the seed only when random is assigned a Random instance or only to get same sequence of results from it to start over again. Class Random's setSeed(long seed) instance method can't execute in the class scope, so the constructor has to set it using the Random constructor with the long seed parameter. The following code shows the changes:

public class RandomDemo { // arbitrary example class name
    // lots of class related stuff may be here...

    // still inside the class scope...
    // private is a good idea unless an external method needs to change it
    private Random randnum = new Random(123456789L);
    // the seed guarantees it will always produce the same sequence
    // of pseudo-random values when the next methods get called
    // for unpredicable sequences, use the following constructor instead:
    // private Random randnum = new Random();

    // lots of code may be here...

    // publicly exposed instance method for getting random number
    // from a sequence determined by seed 123456789L
    // in the range from 0 through i-1
    public int randnum(int i) {
        // don't set the seed in here, or randnum will return the exact same integer
        // for the same value of i on every method call
        // nextInt(i) will give the next value from randnum conforming to range i
        return randnum.nextInt(i);
    } // end randnum

    // lots of more code may be here...

} // end class RandDemo

The above will give you an exact solution to your exact problem, as stated. However, using a mandatory seed seems unusual, given what it does.

If this is for a class project or software testing where the sequence has to be predictable and repeatable, setting the seed to a fixed value makes sense. Otherwise, question the validity of setting the seed to some predetermined value. The following explains more about Random, seeds for Random and why there is a provision for supplying a seed.

Random has two constructors:

Random()

and

Random(long seed)

and an instance method

setSeed(long seed)

that all affect the sequence of numbers obtained from a Random instance. The instance method,

setSeed(long seed)

sets the Random object to the same state it would have been in had it been just instanced with the same seed as the constructor argument. Only the low-order 48 bits of a seed value get used.

If a Random object is instanced without a seed, the seed will be the same as the system time in milliseconds. This ensures that, unless two Random objects are instanced in the same millisecond, they will produce different pseudo-random sequences. Only the low order 48 bits of the seed value gets used. This causes unpredictable pseudo-random sequences. It is not necessary and wasteful of computing resources to get a new instance of Random every time one calls a next method.

Random's seed parameters are provided so that one may instance a Random object that produces a sequence that is repeatable. For a given seed, the sequence of values in next methods are guaranteed to be the same sequence whenever that seed is used. This is useful for testing software that is going to use pseudo-random sequences where results have to be predicable and repeatable. It is not useful for creating different unpredictable pseudo-random sequences in operation.

The statement "it is mandatory that I set the seed" negates any unpredictablity of the Random object's pseudo-random sequences. Is this for a class project or software test where results have to be the same for the same inputs to the program?