JAVA随机数生成Int、Long、Float、Double

摘要:
随机数Int的生成生成无边界的Int@TestpublicvoidtestRandom_generatingIntegerUnbounded()throwsException{intintUnbounded=newRandom().nextInt();System.out.println(intUnbounded);}生成有边界的Int@TestpublicvoidtestRandom_gener

随机数Int的生成

生成无边界的Int

@Test
public void testRandom_generatingIntegerUnbounded() throws Exception {

   int intUnbounded = new Random().nextInt();
   System.out.println(intUnbounded);
}

生成有边界的Int

@Test
public void testRandom_generatingIntegerBounded_withRange() throws Exception {

   int min = 1;
   int max = 10;
   int intBounded = min + ((int) (new Random().nextFloat() * (max - min)));
   System.out.println(intBounded);
}

包含1而不包含10

使用Apache Common Math来生成有边界的Int

@Test
public void testRandom_generatingIntegerBounded_withApacheMath() throws Exception {

   int min = 1;
   int max = 10;
   int intBounded = new RandomDataGenerator().nextInt(min, max);
   System.out.println(intBounded);
}

包含1且包含10

使用Apache Common Lang的工具类来生成有边界的Int

@Test
public void testRandom_generatingIntegerBounded_withApacheLangInclusive() throws Exception {

   int min = 1;
   int max = 10;
   int intBounded = RandomUtils.nextInt(min, max);
   System.out.println(intBounded);
}

包含1而不包含10

使用TreadLocalRandom来生成有边界的Int

@Test
public void testRandom_generatingIntegerBounded_withThreadLocalRandom() throws Exception {

   int min = 1;
   int max = 10;
   int threadIntBound = ThreadLocalRandom.current().nextInt(min, max);
   System.out.println(threadIntBound);
}

包含1而不包含10


随机数Long的生成

生成无边界的Long

@Test
public void testRandom_generatingLongUnbounded() throws Exception {

   long unboundedLong = new Random().nextLong();
   System.out.println(unboundedLong);
}

因为Random类使用的种子是48bits,所以nextLong不能返回所有可能的long值,long是64bits。

生成有边界的Long

@Test
public void testRandom_generatingLongBounded_withRange() throws Exception {

   long min = 1;
   long max = 10;
   long rangeLong = min + (((long) (new Random().nextDouble() * (max - min))));
   System.out.println(rangeLong);
}

以上只会生成1到10的long类型的随机数

使用Apache Commons Math来生成有边界的Long

@Test
public void testRandom_generatingLongBounded_withApacheMath() throws Exception {

   long min = 1;
   long max = 10;
   long rangeLong = new RandomDataGenerator().nextLong(min, max);
   System.out.println(rangeLong);
}

此方式主要使用的RandomDataGenerator类提供的生成随机数的方法

使用Apache Commons Lang的工具类来生成有边界的Long

@Test
public void testRandom_generatingLongBounded_withApacheLangInclusive() throws Exception {

   long min = 1;
   long max = 10;
   long longBounded = RandomUtils.nextLong(min, max);
   System.out.println(longBounded);
}

RandomUtils提供了对java.util.Random的补充

使用ThreadLocalRandom生成有边界的Long

@Test
public void testRandom_generatingLongBounded_withThreadLocalRandom() throws Exception {

   long min = 1;
   long max = 10;
   long threadLongBound = ThreadLocalRandom.current().nextLong(min, max);
   System.out.println(threadLongBound);
}


随机数Float的生成

生成0.0-1.0之间的Float随机数

@Test
public void testRandom_generatingFloat0To1() throws Exception {

   float floatUnbounded = new Random().nextFloat();
   System.out.println(floatUnbounded);
}

以上只会生成包含0.0而不包括1.0的float类型随机数

生成有边界的Float随机数

@Test
public void testRandom_generatingFloatBounded_withRange() throws Exception {

   float min = 1f;
   float max = 10f;
   float floatBounded = min + new Random().nextFloat() * (max - min);
   System.out.println(floatBounded);
}

使用Apache Common Math来生成有边界的Float随机数

@Test
public void testRandom_generatingFloatBounded_withApacheMath() throws Exception {

   float min = 1f;
   float max = 10f;
   float randomFloat = new RandomDataGenerator().getRandomGenerator().nextFloat();
   float generatedFloat = min + randomFloat * (max - min);
   System.out.println(generatedFloat);
}

使用Apache Common Lang来生成有边界的Float随机数

@Test
public void testRandom_generatingFloatBounded_withApacheLang() throws Exception {

   float min = 1f;
   float max = 10f;
   float generatedFloat = RandomUtils.nextFloat(min, max);
   System.out.println(generatedFloat);
}

使用ThreadLocalRandom生成有边界的Float随机数

ThreadLocalRandom类没有提供


随机数Double的生成

生成0.0d-1.0d之间的Double随机数

@Test
public void testRandom_generatingDouble0To1() throws Exception {

   double generatorDouble = new Random().nextDouble();
   System.out.println(generatorDouble);
}

与Float相同,以上方法只会生成包含0.0d而不包含1.0d的随机数

生成带有边界的Double随机数

@Test
public void testRandom_generatingDoubleBounded_withRange() throws Exception {

   double min = 1.0;
   double max = 10.0;
   double boundedDouble = min + new Random().nextDouble() * (max - min);
   System.out.println(boundedDouble);
   assertThat(boundedDouble, greaterThan(min));
   assertThat(boundedDouble, lessThan(max));
}

使用Apache Common Math来生成有边界的Double随机数

@Test
public void testRandom_generatingDoubleBounded_withApacheMath() throws Exception {

   double min = 1.0;
   double max = 10.0;
   double boundedDouble = new RandomDataGenerator().getRandomGenerator().nextDouble();
   double generatorDouble = min + boundedDouble * (max - min);
   System.out.println(generatorDouble);
   assertThat(generatorDouble, greaterThan(min));
   assertThat(generatorDouble, lessThan(max));
}

使用Apache Common Lang生成有边界的Double随机数

@Test
public void testRandom_generatingDoubleBounded_withApacheLang() throws Exception {

   double min = 1.0;
   double max = 10.0;
   double generatedDouble = RandomUtils.nextDouble(min, max);
   System.out.println(generatedDouble);
}

使用ThreadLocalRandom生成有边界的Double随机数

@Test
public void testRandom_generatingDoubleBounded_withThreadLocalRandom() throws Exception {

   double min = 1.0;
   double max = 10.0;
   double generatedDouble = ThreadLocalRandom.current().nextDouble(min, max);
   System.out.println(generatedDouble);
}

JAVA中有多少可以实现随机数的类或方法?

  • java.util.Random 这个类提供了生成Bytes、Int、Long、Float、Double、Boolean的随机数的方法
  • java.util.Math.random 方法提供了生成Double随机数的方法,这个方法的内部实现也是调用了java.util.Random的nextDouble方法,只不过它对多线程进行了更好的支持,在多个线程并发时会减少每个随机数生成器的竞争
  • 第三方工具类,如Apache Common Lang库与Apache Common Math库中提供的随机数生成类,真正使用一行代码来实现复杂的随机数生成
  • java.util.concurrent.ThreadLocalRandom 专为多线程并发使用的随机数生成器,使用的方法为ThreadLocalRandom.current.nextInt(),此类是在JDK1.7中提供的,并且特别适合ForkJoinTask框架,而且在这个类中直接提供了生成有边界的随机数的操作,如public int nextInt(int origin, int bound),这样也可以一行代码来实现复杂的随机数生成了。

最后的总结为单线程中使用java.util.Random类,在多线程中使用java.util.concurrent.ThreadLocalRandom类。

总结

JAVA在不JDK升级中不断在完善API,现在可以使用JDK原生的API写出优雅的代码了。所有的这些测试完整的代码在这里

免责声明:文章转载自《JAVA随机数生成Int、Long、Float、Double》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇postgresql-slony-I同步复制配置步骤HTML之实现下拉式菜单下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

Elasticsearch增删改查 之 —— Update更新

更新操作,一般用这个的,应该不会很多吧!ES本身还是一个倾向于查询检索的框架,对于这种更新的操作,太过频繁总归是不好的。不过阅读本篇后,你可以使用Script对所有的文档执行更新操作,也可以使用doc对部分文档执行更新,也可以使用upsert对不存在的文档执行添加操作。 更新 Update更新操作允许ES获得某个指定的文档,可以通过脚本等操作对该文档进行更...

Apache Drill初探

                                    Apache Drill初探 介绍 Apache Drill是一个开源的,对于Hadoop和NoSQL低延迟的SQL查询引擎。 Apache Drill 实现了 Google's Dremel.那么什么是Google's Dremel?网络中一段描述:Dremel 是Google...

Handler dispatch failed; nested exception is java.lang.AbstractMethodError: Method com/mchange/v2/c3p0/impl/NewProxyResultSet.isClosed()Z is abstract

问题原因:本地用的c3p0为0.9.1.2版本,版本不兼容。 解决方法: maven方式:pom引用升级到 c3p0-0.9.5.5,并且加入 mchange-commons-java-0.2.19 的依赖。 lib方式:直接 下载c3p0-0.9.5.5.bin.zip,解压后将 c3p0-0.9.5.5lib 下的 c3p0-0.9.5.5.jar 和...

apache(二)

apache的三种MPM及配置方式 首先,MPM是Multi-Processing Modules,表示Apache中的多路处理模块,目前在Linux上的Apache 2.2/2.4中包括三种模式:prefork、worker和event模式。 1.worker worker 是多进程多线程模型,一个进程有多个线程,每个线程处理一个连接。与prefork相...

ubuntu14.04安装 Apache2 并配置https

一、安装 Apache2   sudo apt-get update   sudo apt-get install apache2   安装完apache2,默认根目录在/var/www/html 下,点击其下的html 文件,可打开 Apache2的默认页面。 输入 http://localhost/index.html, 也可以通过http://...

本地idea开发mapreduce程序提交到远程hadoop集群执行

https://www.codetd.com/article/664330 https://blog.csdn.net/dream_an/article/details/84342770 通过idea开发mapreduce程序并直接run,提交到远程hadoop集群执行mapreduce。 简要流程:本地开发mapreduce程序–>设置yarn...