一、前言
通过apache的开源cayenne包(2.0.4)中的org.apache.commons.lang.math.RandomUtils工具类,对常见的boolean、int、float、long及double等基本数据类型通过org.apache.commons.lang.math.JVMRandom随机算法进行生成。
二、源码说明
1.RandomUtils工具类
package org.apache.commons.lang.math;@b@@b@import java.util.Random;@b@@b@public class RandomUtils@b@{@b@ public static final Random JVM_RANDOM = new JVMRandom();@b@@b@ public static boolean nextBoolean()@b@ {@b@ return nextBoolean(JVM_RANDOM);@b@ }@b@@b@ public static boolean nextBoolean(Random random)@b@ {@b@ return random.nextBoolean();@b@ }@b@@b@ public static double nextDouble()@b@ {@b@ return nextDouble(JVM_RANDOM);@b@ }@b@@b@ public static double nextDouble(Random random)@b@ {@b@ return random.nextDouble();@b@ }@b@@b@ public static float nextFloat()@b@ {@b@ return nextFloat(JVM_RANDOM);@b@ }@b@@b@ public static float nextFloat(Random random)@b@ {@b@ return random.nextFloat();@b@ }@b@@b@ public static int nextInt()@b@ {@b@ return nextInt(JVM_RANDOM);@b@ }@b@@b@ public static int nextInt(int n)@b@ {@b@ return nextInt(JVM_RANDOM, n);@b@ }@b@@b@ public static int nextInt(Random random)@b@ {@b@ return random.nextInt();@b@ }@b@@b@ public static int nextInt(Random random, int n)@b@ {@b@ return random.nextInt(n);@b@ }@b@@b@ public static long nextLong()@b@ {@b@ return nextLong(JVM_RANDOM);@b@ }@b@@b@ public static long nextLong(Random random)@b@ {@b@ return random.nextLong();@b@ }@b@}
2.JVMRandom随机类
package org.apache.commons.lang.math;@b@@b@import java.util.Random;@b@@b@public final class JVMRandom extends Random@b@{@b@ private boolean constructed = false;@b@@b@ public boolean nextBoolean()@b@ {@b@ return (Math.random() > 0.5D);@b@ }@b@@b@ public void nextBytes(byte[] byteArray)@b@ {@b@ throw new UnsupportedOperationException();@b@ }@b@@b@ public double nextDouble()@b@ {@b@ return Math.random();@b@ }@b@@b@ public float nextFloat()@b@ {@b@ return (float)Math.random();@b@ }@b@@b@ public synchronized double nextGaussian()@b@ {@b@ throw new UnsupportedOperationException();@b@ }@b@@b@ public int nextInt()@b@ {@b@ return nextInt(2147483647);@b@ }@b@@b@ public int nextInt(int n)@b@ {@b@ if (n <= 0) {@b@ throw new IllegalArgumentException(@b@ "Upper bound for nextInt must be positive");@b@ }@b@@b@ return (int)(Math.random() * n);@b@ }@b@@b@ public long nextLong()@b@ {@b@ return nextLong(9223372036854775807L);@b@ }@b@@b@ public static long nextLong(long n)@b@ {@b@ if (n <= 0L) {@b@ throw new IllegalArgumentException(@b@ "Upper bound for nextInt must be positive");@b@ }@b@@b@ return ()(Math.random() * n);@b@ }@b@@b@ public synchronized void setSeed(long seed)@b@ {@b@ if (this.constructed)@b@ throw new UnsupportedOperationException();@b@ }@b@}