java生成随机数的代码_java随机数代码的三种生成方式怎么编写?

随机是某些项目中特有的色彩,如抽奖什么的,本文我们就来了解下java中的随机数代码生成方式。

三种生成方式示例,如下:import java.util.Random;

import java.lang.Math;

/**

* java 的随机数测试程序。共3种获取随机数的方法:

*   (01)、通过System.currentTimeMillis()来获取一个当前时间毫秒数的long型数字。

*   (02)、通过Math.random()返回一个0到1之间的double值。

*   (03)、通过Random类来产生一个随机数,这个是专业的Random工具类,功能强大。

*

* @author skywang

* @email kuiwu-wang@163.com

*/

public class RandomTest

{

public static void main(String args[])

{

// 通过System的currentTimeMillis()返回随机数

testSystemTimeMillis();

// 通过Math的random()返回随机数

testMathRandom();

// 新建“种子为1000”的Random对象,并通过该种子去测试Random的API

testRandomAPIs(new Random(1000), " 1st Random(1000)");

testRandomAPIs(new Random(1000), " 2nd Random(1000)");

// 新建“默认种子”的Random对象,并通过该种子去测试Random的API

testRandomAPIs(new Random(), " 1st Random()");

testRandomAPIs(new Random(), " 2nd Random()");

}

/**

* 返回随机数-01:测试System的currentTimeMillis()

*/

private static void testSystemTimeMillis()

{

// 通过

final long l = System.currentTimeMillis();

// 通过l获取一个[0, 100)之间的整数

final int i = (int)(l % 100);

System.out.printf("\n---- System.currentTimeMillis() ----\n l=%s i=%s\n", l, i);

}

/**

* 返回随机数-02:测试Math的random()

*/

private static void testMathRandom()

{

// 通过Math的random()函数返回一个double类型随机数,范围[0.0, 1.0)

final double d = Math.random();

// 通过d获取一个[0, 100)之间的整数

final int i = (int)(d * 100);

System.out.printf("\n---- Math.random() ----\n d=%s i=%s\n", d, i);

}

/**

* 返回随机数-03:测试Random的API

*/

private static void testRandomAPIs(Random random, String title)

{

final int BUFFER_LEN = 5;

// 获取随机的boolean值

boolean b = random.nextBoolean();

// 获取随机的数组buf[]

byte[] buf = new byte[BUFFER_LEN];

random.nextBytes(buf);

// 获取随机的Double值,范围[0.0, 1.0)

double d = random.nextDouble();

// 获取随机的float值,范围[0.0, 1.0)

float f = random.nextFloat();

// 获取随机的int值

int i1 = random.nextInt();

// 获取随机的[0,100)之间的int值

int i2 = random.nextInt(100);

// 获取随机的高斯分布的double值

double g = random.nextGaussian();

// 获取随机的long值

long l = random.nextLong();

System.out.printf("\n---- %s ----\nb=%s, d=%s, f=%s, i1=%s, i2=%s, g=%s, l=%s, buf=["

, title, b, d, f, i1, i2, g, l);

for (byte bt: buf)

System.out.printf("%s, ", bt);

System.out.println("]");

}

}

以上就是本篇文章的所有内容,更多随机数常见问题请关注我们了解具体。

推荐阅读:

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值