New Random(System.currentTimemillis ()) is the cause of new Random(System.currentTimemillis ())

You only need to

Random random =new Random(System.currentTimeMillis());
Copy the code

I’m going to replace it with something simpler

Random random =new Random();
Copy the code

So, problem solved!

Why is that?

We need only look at the JDk source code to see that the reason is quite simple.

When using New Random(System.currentTimemillis ()), Random calls

    public Random(long seed) {
        if (getClass() == Random.class)
            this.seed = new AtomicLong(initialScramble(seed));
        else {
            // subclass might have overriden setSeed
            this.seed = newAtomicLong(); setSeed(seed); }}Copy the code

With new Random(), is called

    public Random(a) {
        this(seedUniquifier() ^ System.nanoTime());
    }
Copy the code

When we use New Random(System.CurrentTimemillis ()), we will get the current System millisecond time as the seed, so it is possible to get the same time in some simple loops, so randomness is completely lost.

System.nanotime () is the current value of the System timer, which is accurate to micromilliseconds. Note that this method cannot be used to get the current System time, but only as a timer, such as calculating the elapsed time of a piece of code.

So, if you want to use completely Random numbers, just new Random (), don’t pass anything else