Java – get the seed of the random object without passing in the seed?

Does a random object always contain a seed, even if it is not given one? If so, can you get this seed?

Motivation: I want my program to be random, but I want to reproduce it anytime, anywhere What I'm doing now is generating a random number, storing it and seeding it into another random object I use in my actual program So I can find the generated seed if I want to reproduce anything

I want to know this for Java and C #, because these are my main languages. This problem makes me work in both languages several times

Solution

If you do not seed a random constructor, the seed is generated implicitly To seed and reuse it elsewhere in the code or reproduce anything, try the following:

long seed = System.currentTimeMillis();
Random rand = new Random(seed);
System.out.println(seed);
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>