随机数Int的生成 生成无边界的Int
1 2 3 4 5 6 7 8 |
@Test public void testRandom_generatingIntegerUnbounded() throws Exception { int intUnbounded = new Random().nextInt(); System.out.println(intUnbounded); } |
生成有边界的Int
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 使用Apache Common Math来生成有边界的Int
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 使用Apache Common Lang的工具类来生成有边界的Int
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 使用TreadLocalRandom来生成有边界的Int
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 随机数Long的生成 生成无边界的Long
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。 生成有边界的Long
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类型的随机数 使用Apache Commons Math来生成有边界的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类提供的生成随机数的方法 使用Apache Commons Lang的工具类来生成有边界的Long
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的补充 使用ThreadLocalRandom生成有边界的Long
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); } |
随机数Float的生成 生成0.0-1.0之间的Float随机数
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类型随机数 生成有边界的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); } |
使用Apache Common Math来生成有边界的Float随机数
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); } |
使用Apache Common Lang来生成有边界的Float随机数
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生成有边界的Float随机数 ThreadLocalRandom类没有提供 随机数Double的生成 生成0.0d-1.0d之间的Double随机数
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的随机数 生成带有边界的Double随机数
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)); } |
使用Apache Common Math来生成有边界的Double随机数
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)); } |
使用Apache Common Lang生成有边界的Double随机数
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); } |
使用ThreadLocalRandom生成有边界的Double随机数
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); } |
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类。 […]
View Details
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
import java.math.BigDecimal; import java.text.DecimalFormat; import java.text.NumberFormat; public class format { double f = 111231.5585; public void m1() { BigDecimal bg = new BigDecimal(f); double f1 = bg.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue(); System.out.println(f1); } /** * DecimalFormat转换最简便 */ public void m2() { DecimalFormat df = new DecimalFormat("#.00"); System.out.println(df.format(f)); } /** * String.format打印最简便 */ public void m3() { System.out.println(String.format("%.2f", f)); } public void m4() { NumberFormat nf = NumberFormat.getNumberInstance(); nf.setMaximumFractionDigits(2); System.out.println(nf.format(f)); } public static void main(String[] args) { format f = new format(); f.m1(); f.m2(); f.m3(); f.m4(); } } |
from:https://www.cnblogs.com/chenrenshui/p/6128444.html
View Details