newbieprogrammer
newbieprogrammer

Reputation: 868

What is random seed about?

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

Upvotes: 6

Views: 23247

Answers (6)

Raffaele
Raffaele

Reputation: 20875

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.

Upvotes: 9

Nambi
Nambi

Reputation: 12042

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

Upvotes: 0

PetrS
PetrS

Reputation: 1110

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.

Upvotes: 1

Luke Vo
Luke Vo

Reputation: 20658

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.

Upvotes: 3

Niraj Patel
Niraj Patel

Reputation: 2208

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

Upvotes: 0

Codor
Codor

Reputation: 17595

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.

Upvotes: 2

Related Questions