5

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());
    }
}
Community
  • 1
  • 1
newbieprogrammer
  • 848
  • 7
  • 23
  • 46
  • 9
    The answer is always 42. Next time, try reading the javadoc first. – Klas Lindbäck Apr 17 '14 at 07:46
  • 6
    @Klas Lindbäck if I could understand javadoc I wont be asking here. – newbieprogrammer Apr 17 '14 at 07: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. – WASasquatch Apr 17 '14 at 07:53
  • @WASasquatch does it mean every password in such case uses the same salt value ? – newbieprogrammer Apr 17 '14 at 07: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` – WASasquatch Apr 17 '14 at 08:01
  • If you have trouble working out that in the constructor `Random(long seed)` the argument you give is the seed, you have to go back to basics of Java. http://docs.oracle.com/javase/7/docs/api/java/util/Random.html#Random(long) – Peter Lawrey Apr 17 '14 at 09:40
  • @newbieprogrammer Notionally, encryption without a salt uses an empty salt, but I don't see how this is related to your question. Ecryption with a salt will use a random salt. – Peter Lawrey Apr 17 '14 at 09:42
  • [What is a seed in terms of generating a random number?](http://stackoverflow.com/q/14914595/995714) – phuclv Jul 27 '16 at 05:45

6 Answers6

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.

Raffaele
  • 20,627
  • 6
  • 47
  • 86
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.

Luke Vo
  • 17,859
  • 21
  • 105
  • 181
  • Unless you want reproducible results, then you use a fixed seed. – Joey Apr 17 '14 at 07: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 '14 at 07: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 '14 at 08:37
  • 1
    @Јοеу You can also use them in simulation to pull some fancy tricks to [reduce the variance](http://en.wikipedia.org/wiki/Variance_reduction) of estimated model characteristics. – pjs Apr 17 '14 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.

Codor
  • 17,447
  • 9
  • 29
  • 56
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.

PetrS
  • 1,110
  • 14
  • 38
  • 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 '14 at 07:50
  • @PS glass is using System,currentTimeMillis() same as srand(time()) in C++? – newbieprogrammer Apr 17 '14 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 '14 at 10:26
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

Community
  • 1
  • 1
Nambi
  • 11,944
  • 3
  • 37
  • 49
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);
niiraj874u
  • 2,180
  • 1
  • 12
  • 19