new Random()中参数有没有作用?

Random rand =new Random();
rand.nextInt(); 看看bai这样得出的结果du是什么:
package test;
public class Random {
public Random() {
}
public static void main(String[] args) {
java.util.Random random =new java.util.Random();
System.out.println("Integer Max:"+Integer.MAX_VALUE);
System.out.println("-------------------------------");
for(int i=0;i<10;i++){
System.out.println(random.nextInt());
}
System.out.println("-------------------------------");
System.out.println("Integer Min:"+Integer.MIN_VALUE);
}
}
结果如zhi下:
Integer Max:2147483647
-------------------------------
2013044148
1926459619
1860587051
787541552
-1923937766
-573613010
-655315899
-1392871582
826019786
-169246156
-------------------------------
Integer Min:-2147483648


看看随dao即数值是不是在Integer.MAX_VALUE---Integer.MIN_VALUE值之间


再看一个测试:

Random rand =new Random(25);
public class Random {
public Random() {
}
public static void main(String[] args) {
java.util.Random random =new java.util.Random(25);
System.out.println("-------------------------------");
for(int i=0;i<10;i++){
System.out.println(random.nextInt());
}
System.out.println("-------------------------------");
}
}
运行结果如下:
-------------------------------
-1152791334
-222412840
230749894
-5655819
-1523272502
939553597
319875191
1391492874
26074393
1190133908
-------------------------------


可以看出随即的结果仍然在Integer.MAX_VALUE---Integer.MIN_VALUE值之间
初始化值 25并没有作用 只是随机数生成器初始化的种子


继续下一个测试:

rand.nextInt(100);
package test;
public class Random {
public Random() {
}
public static void main(String[] args) {
java.util.Random random =new java.util.Random(70);
System.out.println("-------------------------------");
for(int i=0;i<10;i++){
System.out.println(random.nextInt(100));
}
System.out.println("-------------------------------");
}
}
运行结果如下:
-------------------------------
46
90
30
15
41
94
94
64
65
99
-------------------------------
可以看出 Random rand =new Random(25);
初始化时 25并没有起直接作用
rand.nextInt(100); 中的100是随机数的上限
产生的随机数为0-100的整数 不包括100

转载:https://zhidao.baidu.com/question/87159986.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值