都为你整理好了,5种Java 随机方式对比!你都知道吗?
1. Math.random() 静态方法 产生的随机数是 0 - 1 之间的一个 double,即 0 <= random <= 1。 for (int i = 0; i < 10; i++) { System.out.println(Math.random()); } 结果: 0.3598613895606426 0.2666778145365811 0.25090731064243355 0.011064998061666276 0.600686228175639 0.9084006027629496 0.12700524654847833 0.6084605849069343 0.7290804782514261 0.9923831908303121 实现原理: When this method is first called, it creates a single new pseudorandom-number generator, exactly as if by the expression new java.util.Random() This new pseudorandom-number generator is used thereafter for all calls to this method and is used nowhere else.