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 Math三、Apache Common Lang三、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>