6

For example the code below. It has a random class. However it always produce the same output everywhere . In this case which item is the seed?

source: link

import java.util.Random;
public class RandomTest {
    public static void main(String[] s) {
        Random rnd1 = new Random(42);
        Random rnd2 = new Random(42);

        System.out.println(rnd1.nextInt(100)+" - "+rnd2.nextInt(100));
        System.out.println(rnd1.nextInt()+" - "+rnd2.nextInt());
        System.out.println(rnd1.nextDouble()+" - "+rnd2.nextDouble());
        System.out.println(rnd1.nextLong()+" - "+rnd2.nextLong());
    }
}
8
  • 9
    The answer is always 42. Next time, try reading the javadoc first. Apr 17, 2014 at 7:46
  • 6
    @Klas Lindbäck if I could understand javadoc I wont be asking here. Apr 17, 2014 at 7:51
  • 1
    The anser to the universe and everything is 42. Its a joke. The random function uses seeds for token creation like salt in password encryption, but there to add a non-stored key. Here you can use the same seed (id) to get a result. Apr 17, 2014 at 7:53
  • @WASasquatch does it mean every password in such case uses the same salt value ? Apr 17, 2014 at 7:56
  • Usually they use a random salt as there intention is not a seed. Its like the opposite. Here we intend to resuse the seed, there they just salt it, spice it up, make it harder to decrypt. So they'll use microtime based on the very second and date of execution, ie: .currentTimeMillis Apr 17, 2014 at 8:01

6 Answers 6

9

42 is the seed, as the very same Javadoc says. So, what is a seed? A random number is seldom truly random - often it's a pseudo-random instead. This means it's generated from a function, which is said PRNG (pseudo random number genrator). Being generated from a function, in turn, means that the output is not random anymore, since it's predictable!

However, depending on your needs, this pseudo-randomness may be enough - I said enough because generating random bit is expensive, and I'm not talking about time or memory, but about money (see this link on wikipedia). So, for example, if you need a random value to place enemies in your game, a pseudo-random number is ok - but if your are building security-related software, you want to use a true random number, or at least a cryptographically secure PRNG.

How can we describe a PRNG, like the one used in Math.random()? It's a function, initialized with a seed S that returns an array of values A. Note that, for each integer S, is defined one and only one array A. For example (values are not actual):

                first call     second call     third call
seed: 14329            .18             .82             .5
seed:  3989             .7             .02            .93

So you seed you PRNG with some known value when you want its result to be predictable - for example for testing purposes or to ensure that, each time you run level 1 in your game, the enemies are always placed in the same (pseudo) random places - otherwise you don't need to explicitely pass a seed.

3

Random Seed on Wikipedia:

A random seed (or seed state, or just seed) is a number (or vector) used to initialize a pseudorandom number generator.

In other word, it is the number from which a seem-to-be-random sequence will be generated. Therefore, if you use the same number, the senquence will always be the same.

In practice, we usually use System Time as seed.

4
  • Unless you want reproducible results, then you use a fixed seed.
    – Joey
    Apr 17, 2014 at 7:51
  • It's true, but I don't really see what its real usage is (except in testing). Generate a fixed sequence? I would bet in another more reliable way. Generate random sequence is usually more performance-heavy.
    – Luke Vo
    Apr 17, 2014 at 7:55
  • 1
    You want them in simulations, which is a large part of why you're using PRNGs there (storing just a seed for reproducible results instead of all random numbers needed :-))
    – Joey
    Apr 17, 2014 at 8:37
  • 1
    @Јοеу You can also use them in simulation to pull some fancy tricks to reduce the variance of estimated model characteristics.
    – pjs
    Apr 17, 2014 at 13:35
2

The seed is given as the argument of the constructor of Random; using the same seed will yield the same sequence of numbers. However this is discussed under the link in thet question.

1

In this case the seed is 42. This is the reason for the same output - you use the same seed. You can use for example

  Random rnd1 = new Random(System.currentTimeMillis())

for different outputs.

3
  • 1
    He'd better not specify any seed. Using System.currentTimeMillis() has a good chance of producing the same seed for two consequent invocations of the method.
    – JB Nizet
    Apr 17, 2014 at 7:50
  • @PS glass is using System,currentTimeMillis() same as srand(time()) in C++? Apr 17, 2014 at 10:13
  • I do not know the C++ very well, but I think that yes. It is very similar, but I am not sure. This question should be answered by someone who knows both languages (Java and C++).
    – PetrS
    Apr 17, 2014 at 10:26
0

The seed is the initial value of the internal state of the pseudorandom number generator which is maintained by method next(int).

The invocation new Random(seed) is equivalent to:

 Random rnd = new Random();
 rnd.setSeed(seed);
0

From the Java documentation in the Random class

Creates a new random number generator using a single long seed. The seed is the initial value of the internal state of the pseudorandom number generator which is maintained by method next(int).

The invocation new Random(seed) is equivalent to:

Random rnd = new Random(); rnd.setSeed(seed);

So 42 is the seed given to the new Random() in your example

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.