1. 程式人生 > >JAVA生成隨機數

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類。