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);
java生成随机数的方法}
包含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 = Int(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 = Long(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 = Float(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 = Double(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库中提供的随机数⽣成类,真正使⽤⼀⾏代码来实现复杂的随机数⽣成
urrent.ThreadLocalRandom 专为多线程并发使⽤的随机数⽣成器,使⽤的⽅法为
Int(),此类是在JDK1.7中提供的,并且特别适合ForkJoinTask框架,⽽且在这个类中直接提供了⽣成有边界的随机数的操作,如public int nextInt(int origin, int bound),这样也可以⼀⾏代码来实现复杂的随机数⽣成了。
最后的总结为单线程中使⽤java.util.Random类,在多线程中使⽤urrent.ThreadLocalRandom类。
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论