Move instance r outside the for loop, as shown in the figure:
Random r = new Random(); for (int i = 0; i < trials; i++) { : }
Now you create a new one every time the cycle repeats, and since the seed is based on time (milliseconds), you are likely to get a lot with the same seed.
This is almost certainly what distorts your results.
So, yes, this is a mistake, just in your code, and not in Java. This occurs in approximately 99.9999% of cases when people ask this question, because Java itself is constantly tested by millions of people around the world, and this fragment has been tested, well, only you :-)
source share