Java生成随机数

来源:互联网 发布:梦幻诛仙手游数据互通 编辑:程序博客网 时间:2024/05/16 14:03

一、生成0~N的随机整数

例如:生成0~1000的随机整数

Random random = new Random();int n = random.nextInt();

或者
int n = (int)Math.random()*100;

二、生成M~N的随机整数

public int getRandomNumber(int m, int n) {    Long temp = Math.round(Math.random() * (n- m) + m);    return temp.intValue();}

或者

public int getRandomNumber(int min, int max) {    int temp = (int)(Math.random() * (n-m) + m);    return temp;}
0 0