0

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

I'm using the Java's Random class and the nextInt() method to get random numbers. But it seems like the numbers are always in the same order. Is there a way to fix this? I know some random generators take in a seed value, then you use the system timer for the seed.

Code:

Community
  • 1
  • 1
Ted pottel
  • 6,869
  • 21
  • 75
  • 134

4 Answers4

0

This should give you random number evry time:

Time t = new Time();
t.setToNow();
mRandom = new Random(t.toMillis(false));
int rand = mRandom.nextInt(10);
Shaunak
  • 17,377
  • 5
  • 53
  • 84
  • From the [Random()](http://developer.android.com/reference/java/util/Random.html#Random%28%29) constructor documentation: "The initial state (that is, the seed) is _partially_ based on the current time of day in milliseconds." So setting it to _wholly_ based on the current time, seems less random. – Sam Oct 12 '12 at 23:13
0

create the Random object only once (for example at application's onCreate) , and it should give you relatively random results each time you start the app.

android developer
  • 114,585
  • 152
  • 739
  • 1,270
0

This is what you really need to refer to https://stackoverflow.com/a/11705615/1143977. Quoting my answer from the reference link.

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.

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.

Community
  • 1
  • 1
VendettaDroid
  • 3,131
  • 2
  • 28
  • 41
  • 1
    If you can cut & paste an answer from another question, you should flag this question as a duplicate. (Thank you for citing the source though!) – Sam Oct 12 '12 at 23:14
  • Thank you for taking the time to explain the random functionlty to me, – Ted pottel Oct 15 '12 at 13:55
0

To generate random number in Android, class java.util.Random can be used.

This class java.util.Random provides methods that generates pseudo-random numbers of different types, such as int, long, double, and float.

It support two public constructor:

  1. Random() - Construct a random generator with the current time of day in milliseconds as the initial state.

  2. Random(long seed) - Construct a random generator with the given seed as the initial state.

For more details see these links:

Generate random number in Android

Generate Random number, Random()

K_Anas
  • 31,226
  • 9
  • 68
  • 81