Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does the use of Random with a hardcoded seed always produce the same results? [duplicate]

Tags:

java

random

The following simple program in Java uses the java.util.Random class such that it always displays "hello world". The code snippet can be seen below.

package nomain;

import java.util.Random;

final public class J
{
    public static String randomString(int seed)
    {
        Random rand = new Random(seed);
        StringBuilder sb = new StringBuilder();

        for(int i=0;;i++)
        {
            int n=rand.nextInt(27);
            if (n==0)
            {
                break;
            }
            sb.append((char) ('`'+n));
        }
        return sb.toString();
    }

    public static void main(String args[])
    {
        System.out.println(randomString(-229985452)+' '+randomString(-147909649));
    }
}

There is some surprise in that it always displays "hello world" even if the Random class is used that causes the random numbers to generate hence, the numbers should be changed on each run and the corresponding characters should be changed accordingly but it always displays only one stable string which is as mentioned above "hello world". Why does it happen?

like image 873
Bhavesh Avatar asked Nov 08 '11 00:11

Bhavesh


People also ask

How do random number seeds work?

A random seed is a starting point in generating random numbers. A random seed specifies the start point when a computer generates a random number sequence. This can be any number, but it usually comes from seconds on a computer system's clock (Henkemans & Lee, 2001).

How are random seeds generated?

Random seeds are often generated from the state of the computer system (such as the time), a cryptographically secure pseudorandom number generator or from a hardware random number generator.

How does seed work in Java random?

The "random" numbers generated by the mathematical algorithm are given a starting number (called the "seed") and always generates the same sequence of numbers. Since the same sequence is generated each time the seed remains the same, the sequence of random numbers is referred to as being a pseudo-random sequence.

What will happen if the seed value of a PRNG is constant?

Setting the initial state is called seeding the PRNG. Calling a PRNG in the same initial state, either without seeding it explicitly or by seeding it with a constant value, results in generating the same sequence of random numbers in different runs of the program.


1 Answers

The answer is the parameter that's being passed in. That's used to seed the random number generator.

Random rand = new Random(seed);

PRNGs are not truly random - they are deterministic, but are designed to simulate randomness. For this reason they are better termed "pseudo-random number generators".

For a given seed, a PRNG will always generate the same number. It then likely uses its last result as an input to the next value, so by seeding the PRNG with a known value, you will always produce a known sequence of "random" numbers.

The numbers -229985452 and -147909649 are known to be seeds which will cause that particular PRNG to produce a sequence of 5 numbers that can be interpreted as "hello" and "world". If you were to change those numbers, you would get different 5-character sequences, but they would be the same for every run of the program, so long as the seed remains the same.

like image 56
Chris Heald Avatar answered Oct 08 '22 14:10

Chris Heald