Java生成随机数的三种方式
package cn.zytao.taosir.random;import java.util.Random;
public class RandomDemo {
private static Integer byNewRandom() {
Random r=new Random();
return r.nextInt(100);
}
private static Integer byMathRandom() {
int max=100,min=1;
return (int)(Math.random()*(max-min)+min);
}
private static Integer byCurrentTimeMillis() {
int max=100,min=1;
long randomNum=System.currentTimeMillis();
return (int)(randomNum%(max-min)+min);
}
public static void main(String[] args) {
for (int i = 0; i < 10; i++) {
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
//System.out.println(byNewRandom());
//System.out.println(byMathRandom());
System.out.println(byCurrentTimeMillis());
}
}
}
以上是 Java生成随机数的三种方式 的全部内容, 来源链接: utcz.com/z/392410.html