package IO;
import java.util.Random;
public class TestRandom {
public static void main(String[] args) {
// 案例1
System.out.println("Math.random()=" + Math.random());// 结果是个double类型的值,区间为[0.0,1.0)
int num = (int) (Math.random() * 3); // 注意不要写成(int)Math.random()*3,这个结果为0,因为先执行了强制转换
System.out.println("num=" + num);
/**
* 输出结果为:
*
* Math.random()=0.02909671613289655
* num=0
*
*/
}
}
Random rand =new Random(25); int i; i=rand.nextInt(100);
package IO;
import java.util.ArrayList;
import java.util.Random;
public class TestRandom {
public static void main(String[] args) {
// 案例2
// 对于种子相同的Random对象,生成的随机数序列是一样的。
Random ran1 = new Random(10);
System.out.println("使用种子为10的Random对象生成[0,10)内随机整数序列: ");
for (int i = 0; i < 10; i++) {
System.out.print(ran1.nextInt(10) + " ");
}
System.out.println();
Random ran2 = new Random(10);
System.out.println("使用另一个种子为10的Random对象生成[0,10)内随机整数序列: ");
for (int i = 0; i < 10; i++) {
System.out.print(ran2.nextInt(10) + " ");
}
/**
* 输出结果为:
*
* 使用种子为10的Random对象生成[0,10)内随机整数序列:
* 3 0 3 0 6 6 7 8 1 4
* 使用另一个种子为10的Random对象生成[0,10)内随机整数序列:
* 3 0 3 0 6 6 7 8 1 4
*
*/
// 案例3
// 在没带参数构造函数生成的Random对象的种子缺省是当前系统时间的毫秒数。
Random r3 = new Random();
System.out.println();
System.out.println("使用种子缺省是当前系统时间的毫秒数的Random对象生成[0,10)内随机整数序列");
for (int i = 0; i < 10; i++) {
System.out.print(r3.nextInt(10)+" ");
}
/**
* 输出结果为:
*
* 使用种子缺省是当前系统时间的毫秒数的Random对象生成[0,10)内随机整数序列
* 1 1 0 4 4 2 3 8 8 4
*
*/
// 另外,直接使用Random无法避免生成重复的数字,如果需要生成不重复的随机数序列,需要借助数组和集合类
ArrayList list=new TestRandom().getDiffNO(10);
System.out.println();
System.out.println("产生的n个不同的随机数:"+list);
}
/**
* 生成n个不同的随机数,且随机数区间为[0,10)
* @param n
* @return
*/
public ArrayList getDiffNO(int n){
// 生成 [0-n) 个不重复的随机数
// list 用来保存这些随机数
ArrayList list = new ArrayList();
Random rand = new Random();
boolean[] bool = new boolean[n];
int num = 0;
for (int i = 0; i < n; i++) {
do {
// 如果产生的数相同继续循环
num = rand.nextInt(n);
} while (bool[num]);
bool[num] = true;
list.add(num);
}
return list;
}
}
int n2 = r.nextInt(10);//方法一 n2 = Math.abs(r.nextInt() % 10);//方法二
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有