JAVA生成随机数

随机数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)