1 2 3 4 5 6 7 8 |
@Test public void testRandom_generatingIntegerUnbounded() throws Exception { int intUnbounded = new Random().nextInt(); System.out.println(intUnbounded); } |
1 2 3 4 5 6 7 8 9 10 |
@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
1 2 3 4 5 6 7 8 9 |
@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
1 2 3 4 5 6 7 8 9 10 |
@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
1 2 3 4 5 6 7 8 9 10 |
@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
1 2 3 4 5 6 7 8 |
@Test public void testRandom_generatingLongUnbounded() throws Exception { long unboundedLong = new Random().nextLong(); System.out.println(unboundedLong); } |
因为Random类使用的种子是48bits,所以nextLong不能返回所有可能的long值,long是64bits。
1 2 3 4 5 6 7 8 9 10 |
@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类型的随机数
1 2 3 4 5 6 7 8 9 10 |
@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类提供的生成随机数的方法
1 2 3 4 5 6 7 8 9 10 |
@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的补充
1 2 3 4 5 6 7 8 9 10 |
@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); } |
1 2 3 4 5 6 7 8 |
@Test public void testRandom_generatingFloat0To1() throws Exception { float floatUnbounded = new Random().nextFloat(); System.out.println(floatUnbounded); } |
以上只会生成包含0.0而不包括1.0的float类型随机数
1 2 3 4 5 6 7 8 9 10 |
@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); } |
1 2 3 4 5 6 7 8 9 10 |
@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); } |
1 2 3 4 5 6 7 8 9 10 |
@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类没有提供
1 2 3 4 5 6 7 8 |
@Test public void testRandom_generatingDouble0To1() throws Exception { double generatorDouble = new Random().nextDouble(); System.out.println(generatorDouble); } |
与Float相同,以上方法只会生成包含0.0d而不包含1.0d的随机数
1 2 3 4 5 6 7 8 9 10 11 12 |
@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)); } |
1 2 3 4 5 6 7 8 9 10 11 12 13 |
@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)); } |
1 2 3 4 5 6 7 8 9 10 |
@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); } |
1 2 3 4 5 6 7 8 9 10 |
@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); } |
public int nextInt(int origin, int bound)
,这样也可以一行代码来实现复杂的随机数生成了。最后的总结为单线程中使用java.util.Random类,在多线程中使用java.util.concurrent.ThreadLocalRandom类。
JAVA在不JDK升级中不断在完善API,现在可以使用JDK原生的API写出优雅的代码了。所有的这些测试完整的代码在这里
from:https://www.cnblogs.com/tomyLi/p/JAVA-sui-ji-shu-sheng-chengIntLongFloatDouble.html