Java-随机数工具类
Posted wang1001
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Java-随机数工具类相关的知识,希望对你有一定的参考价值。
1 import java.util.Random; 2 import java.util.concurrent.ThreadLocalRandom; 3 import org.apache.commons.lang3.RandomUtils; 4 import org.apache.commons.math3.random.RandomDataGenerator; 5 6 /** 7 * @ClassName: RandomUtil 8 * @Description: 随机数工具类 9 * (分别使用java.util.Random、Apache Common Math3、Apache Common Lang3、TreadLocalRandom) 10 */ 11 public class RandomUtil { 12 /** 13 * 随机数Int的生成 14 */ 15 // 随机数生成无边界的Int 16 public static int getRandomForIntegerUnbounded() { 17 int intUnbounded = new Random().nextInt(); 18 System.out.println(intUnbounded); 19 return intUnbounded; 20 } 21 22 // 生成有边界的Int 23 public static int getRandomForIntegerBounded(int min, int max) { 24 int intBounded = min + ((int) (new Random().nextFloat() * (max - min))); 25 System.out.println(intBounded); 26 return intBounded; 27 } 28 29 // 包含1而不包含10 30 // 使用Apache Common Math3来生成有边界的Int 31 public static int getRandomForIntegerBounded2(int min, int max) { 32 int intBounded = new RandomDataGenerator().nextInt(min, max); 33 System.out.println(intBounded); 34 return intBounded; 35 } 36 37 // 包含1且包含10 38 // 使用Apache Common Lang3的工具类来生成有边界的Int 39 public static int getRandomForIntegerBounded3(int min, int max) { 40 int intBounded = RandomUtils.nextInt(min, max); 41 System.out.println(intBounded); 42 return intBounded; 43 } 44 45 // 使用TreadLocalRandom来生成有边界的Int,包含min而不包含max 46 public static int getRandomForIntegerBounded4(int min, int max) { 47 int threadIntBound = ThreadLocalRandom.current().nextInt(min, max); 48 System.out.println(threadIntBound); 49 return threadIntBound; 50 } 51 52 /** 53 * 随机数Long的生成 54 */ 55 // 随机数生成无边界的Long 56 public static long getRandomForLongUnbounded() { 57 long unboundedLong = new Random().nextLong(); 58 System.out.println(unboundedLong); 59 return unboundedLong; 60 } 61 62 // 因为Random类使用的种子是48bits,所以nextLong不能返回所有可能的long值,long是64bits。 63 // 使用Random生成有边界的Long 64 public static long getRandomForLongBounded(long min, long max) { 65 long rangeLong = min + (((long) (new Random().nextDouble() * (max - min)))); 66 System.out.println(rangeLong); 67 return rangeLong; 68 } 69 70 // 使用Apache Commons Math3来生成有边界的Long(RandomDataGenerator类提供的生成随机数的方法) 71 public static long getRandomForLongBounded2(long min, long max) { 72 long rangeLong = new RandomDataGenerator().nextLong(min, max); 73 System.out.println(rangeLong); 74 return rangeLong; 75 } 76 77 // 使用Apache Commons Lang3的工具类来生成有边界的Long(RandomUtils提供了对java.util.Random的补充) 78 public static long getRandomForLongBounded3(long min, long max) { 79 long longBounded = RandomUtils.nextLong(min, max); 80 System.out.println(longBounded); 81 return longBounded; 82 } 83 84 // 使用ThreadLocalRandom生成有边界的Long 85 public static long getRandomForLongBounded4(long min, long max) { 86 long threadLongBound = ThreadLocalRandom.current().nextLong(min, max); 87 System.out.println(threadLongBound); 88 return threadLongBound; 89 } 90 91 /** 92 * 随机数Float的生成 93 */ 94 // 随机数Float的生成生成0.0-1.0之间的Float随机数 95 public static float getRandomForFloat0To1() { 96 float floatUnbounded = new Random().nextFloat(); 97 System.out.println(floatUnbounded); 98 return floatUnbounded; 99 } 100 101 // 以上只会生成包含0.0而不包括1.0的float类型随机数生成有边界的Float随机数 102 public static float getRandomForFloatBounded(float min, float max) { 103 float floatBounded = min + new Random().nextFloat() * (max - min); 104 System.out.println(floatBounded); 105 return floatBounded; 106 } 107 108 // 使用Apache Common Math来生成有边界的Float随机数 109 public static float getRandomForFloatBounded2(float min, float max) { 110 float randomFloat = new RandomDataGenerator().getRandomGenerator().nextFloat(); 111 float generatedFloat = min + randomFloat * (max - min); 112 System.out.println(generatedFloat); 113 return generatedFloat; 114 } 115 116 // 使用Apache Common Lang来生成有边界的Float随机数 117 public static float getRandomForFloatBounded3(float min, float max) { 118 float generatedFloat = RandomUtils.nextFloat(min, max); 119 System.out.println(generatedFloat); 120 return generatedFloat; 121 } 122 123 // 使用ThreadLocalRandom生成有边界的Float随机数 124 // ThreadLocalRandom类没有提供 125 126 /** 127 * 随机数Double的生成 128 */ 129 // 生成0.0d-1.0d之间的Double随机数 130 public static double getRandomForDouble0To1() { 131 double generatorDouble = new Random().nextDouble(); 132 System.out.println(generatorDouble); 133 return generatorDouble; 134 } 135 136 // 与Float相同,以上方法只会生成包含0.0d而不包含1.0d的随机数生成带有边界的Double随机数 137 public static double getRandomForDoubleBounded(double min, double max) { 138 double boundedDouble = min + new Random().nextDouble() * (max - min); 139 System.out.println(boundedDouble); 140 return boundedDouble; 141 } 142 143 // 使用Apache Common Math来生成有边界的Double随机数 144 public static double getRandomForDoubleBounded2(double min, double max) { 145 double boundedDouble = new RandomDataGenerator().getRandomGenerator().nextDouble(); 146 double generatorDouble = min + boundedDouble * (max - min); 147 System.out.println(generatorDouble); 148 return generatorDouble; 149 } 150 151 // 使用Apache Common Lang生成有边界的Double随机数 152 public static double getRandomForDoubleBounded3(double min, double max) { 153 double generatedDouble = RandomUtils.nextDouble(min, max); 154 System.out.println(generatedDouble); 155 return generatedDouble; 156 } 157 158 // 使用ThreadLocalRandom生成有边界的Double随机数 159 public static double getRandomForDoubleBounded4(double min, double max) { 160 double generatedDouble = ThreadLocalRandom.current().nextDouble(min, max); 161 System.out.println(generatedDouble); 162 return generatedDouble; 163 } 164 }
1 //相关依赖 2 <dependency> 3 <groupId>org.apache.commons</groupId> 4 <artifactId>commons-lang3</artifactId> 5 <version>3.3.2</version> 6 </dependency> 7 <!-- commons-math3 --> 8 <dependency> 9 <groupId>org.apache.commons</groupId> 10 <artifactId>commons-math3</artifactId> 11 <version>3.6.1</version> 12 </dependency>
以上是关于Java-随机数工具类的主要内容,如果未能解决你的问题,请参考以下文章