Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I get the seed from a Random in Java?

I am creating a deep clone for some object. The object contains a Random.

Is it good practice to retrieve the seed from the Random? If so, how? There isn't a Random.getSeed().

like image 234
Marnix Avatar asked May 14 '11 10:05

Marnix


People also ask

How do you generate a random seed in Java?

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

How do you get a Java seed?

In Java Edition, the player can enter the command /seed to view the world's seed. This command is available in singleplayer worlds even if cheats are off. The player can also select 'Re-create' in the Worlds menu to see the seed.

What is rand () seed?

In Golang, the rand. Seed() function is used to set a seed value to generate pseudo-random numbers. If the same seed value is used in every execution, then the same set of pseudo-random numbers is generated. In order to get a different set of pseudo-random numbers, we need to update the seed value.

What is the seed value of a random number generator?

When you use statistical software to generate random numbers, you usually have an option to specify a random number seed. A seed is a positive integer that initializes a random-number generator (technically, a pseudorandom-number generator). A seed enables you to create reproducible streams of random numbers.


1 Answers

A much more simple way of getting a seed is to generate one and store that as the seed. I am using this method for a game and want to give the player the option to generate the exact same world if he wishes too. So first I create a Random object without a seed then let that one generate a random number and use that in another random object as the seed. Whenever the player want the seed of the level I have it stored somewhere. By default the game is still random.

    Random rand = new Random();     //Store a random seed     long seed = rand.nextLong();     //Set the Random object seed     rand.setSeed(seed);      //do random stuff...      //Wonder what the seed is to reproduce something?     System.out.println(seed); 
like image 97
Madmenyo Avatar answered Oct 11 '22 18:10

Madmenyo