使用Random生成各种要求的随机数

来源:互联网 发布:人才外包知乎 编辑:程序博客网 时间:2024/05/22 13:54

/**
 * 随机数的使用方法,以及案例分析
 */
import java.util.Random;


public class TestRandom {
public static void main(String[] args) {
// 测试生成的随机数
random0();
System.out.println("<----------分割线-----------<");
TestRandom10.random10();
System.out.println("<----------分割线-----------<");
TestRandom20.random20();
System.out.println("<----------分割线-----------<");
RandomTest.random30();
System.out.println("<----------分割线-----------<");
RandomTest_Repeat.random40();
}


public static void random0() {
// 生成随机数
/*
* Random()使用当前时间即System.currentTimeMillis()作为发生器的种子, Random(long
* seed)使用指定的seed作为发生器的种子。 随机数发生器(Random)对象产生以后,
* 通过调用不同的method:nextInt()、nextLong()、nextFloat()、nextBoolean()、
* nextDouble()等获得不同类型随机数。
*/
Random random1 = new Random(100);
System.out.println(random1.nextInt());
System.out.println(random1.nextFloat());
System.out.println(random1.nextLong());
System.out.println(random1.nextDouble());
System.out.println(random1.nextBoolean());
System.out.println("<---------------------->");
Random random2 = new Random(100);
System.out.println(random2.nextInt());
System.out.println(random2.nextFloat());
System.out.println(random2.nextLong());
System.out.println(random2.nextDouble());
System.out.println(random2.nextBoolean());
}
}
/*
 * 如果2个Random对象使用相同的种子(比如都是100),
 *  并且以相同的顺序调用相同的函数,那它们返回值完全相同
 */
/*输出结果为(固定的不会改变)
  -1193959466
0.7346627
3596673253889729385
0.6671595726539502
true
<---------------------->
-1193959466
0.7346627
3596673253889729385
0.6671595726539502
true
  * */




class TestRandom10 {
// 指定范围内的随机数
// 随机数控制在某个范围内,使用模数运算符%
public static void random10() {
Random random = new Random();
for (int i = 0; i < 10; i++) {
System.out.println(Math.abs(random.nextInt()));
// 获得的随机数有正有负的,用Math.abs使获取数据范围为非负数
}
}
}
/*输出结果为(随机的会改变的)
* 1943820827
734173177
1632591857
251763452
1783526690
1281819961
1030826312
2067682504
2103596446
968404693*/


class TestRandom20 {
// 获取指定范围内的不重复随机数
public static void random20() {
int[] intRet = new int[6];//声明一个数组,用来存放随机数
int intRd = 0; // 存放随机数
int count = 0; // 记录生成的随机数个数
int flag = 0; // 是否已经生成过标志
while (count < 6) {
//使用while循环生成六组随机数
Random rdm = new Random(System.currentTimeMillis());
//获得当前时间的毫秒数
//拿他对一些数取模,就可以把他限制在一个范围之内啦
intRd = Math.abs(rdm.nextInt()) + 1;
//获得的随机数有正有负的,用Math.abs使获取数据范围为非负数;结果加一
for (int i = 0; i < count; i++) {
if (intRet[i] == intRd) {
flag = 1;
break;
} else {
flag = 0;
}//防止生成相同的随机数
}
if (flag == 0) {
intRet[count] = intRd;
count++;//随机数的下标或者是个数
}
}
for (int t = 0; t < 6; t++) {
System.out.println(t + "->" + intRet[t]);
}
}
}
/*输出结果为(随机改变不确定)
*0->228849512
1->228464763
2->229619010
3->226925767
4->228080014
5->227695265 
*/


class RandomTest {
// 使用不带参数的Random()构造函数
public static void random30() {
Random r = new Random();
for (int i = 0; i < 10; i++) {
System.out.println(r.nextInt());
}
}
// 使用不带参数的Random()构造函数产生的随机数不会重复
}
/*输出结果为(随机改变的)
* 20376739
1424828939
-752116623
-1788775546
1730616066
-1872234138
1339748032
-1218211211
-1154740660
804332171
*/


class RandomTest_Repeat {
// 为Random设置种子数
public static void random40() {
Random r = new Random(10);
// 10为种子数;在种子数的基础上进行一定的变换,从而产生需要的随机数字。
for (int i = 0; i < 10; i++) {
System.out.println(r.nextInt());
}
}
// 测试结果也不会改变!
}
/*输出结果为(固定不变的)
  -1157793070
1913984760
1107254586
1773446580
254270492
-1408064384
1048475594
1581279777
-778209333
1532292428
*/
0 0
原创粉丝点击