List of usage examples for java.util Random Random
public Random(long seed)
From source file:Main.java
public static void genRandom(long seed, int factor, int offset, float array[], int stride, int skip) { Random r = new Random(seed); for (int i = 0; i < array.length / stride; i++) { for (int j = 0; j < stride; j++) { if (j >= stride - skip) array[i * stride + j] = 0; else//from ww w. java 2 s . c om array[i * stride + j] = r.nextFloat() * factor + offset; } } }
From source file:Main.java
/** * Fills the array with random longs. If signed is true, negative values can be generated. * The values will fit within 'numberOfBits'. This is useful for conversion tests. *///ww w .j ava 2 s . com public static void genRandomLongs(long seed, long array[], boolean signed, int numberOfBits) { long positiveMask = numberOfBits == 64 ? -1 : ((1l << numberOfBits) - 1); long negativeMask = ~positiveMask; Random r = new Random(seed); for (int i = 0; i < array.length; i++) { long l = r.nextLong(); if (signed && l < 0) { l = l | negativeMask; } else { l = l & positiveMask; } array[i] = l; } // Seed a few special numbers we want to be sure to test. array[r.nextInt(array.length)] = 0l; array[r.nextInt(array.length)] = 1l; array[r.nextInt(array.length)] = positiveMask; if (signed) { array[r.nextInt(array.length)] = negativeMask; array[r.nextInt(array.length)] = -1; } }
From source file:Main.java
/** * Fills the array with random floats. Values will be between min (inclusive) and * max (inclusive).//from www . j a v a 2s . co m */ public static void genRandomFloats(long seed, float min, float max, float array[], boolean includeExtremes) { Random r = new Random(seed); int minExponent = Math.min(Math.getExponent(min), 0); int maxExponent = Math.max(Math.getExponent(max), 0); if (minExponent < -6 || maxExponent > 6) { // Use an exponential distribution int exponentDiff = maxExponent - minExponent; for (int i = 0; i < array.length; i++) { float mantissa = r.nextFloat(); int exponent = minExponent + r.nextInt(maxExponent - minExponent); int sign = (min >= 0) ? 1 : 1 - r.nextInt(2) * 2; // -1 or 1 float rand = sign * mantissa * (float) Math.pow(2.0, exponent); if (rand < min || rand > max) { continue; } array[i] = rand; } } else { // Use a linear distribution for (int i = 0; i < array.length; i++) { float rand = r.nextFloat(); array[i] = min + rand * (max - min); } } // Seed a few special numbers we want to be sure to test. for (int i = 0; i < sInterestingDoubles.length; i++) { float f = (float) sInterestingDoubles[i]; if (min <= f && f <= max) { array[r.nextInt(array.length)] = f; } } array[r.nextInt(array.length)] = min; array[r.nextInt(array.length)] = max; if (includeExtremes) { array[r.nextInt(array.length)] = Float.NaN; array[r.nextInt(array.length)] = Float.POSITIVE_INFINITY; array[r.nextInt(array.length)] = Float.NEGATIVE_INFINITY; array[r.nextInt(array.length)] = Float.MIN_VALUE; array[r.nextInt(array.length)] = Float.MIN_NORMAL; array[r.nextInt(array.length)] = Float.MAX_VALUE; array[r.nextInt(array.length)] = -Float.MIN_VALUE; array[r.nextInt(array.length)] = -Float.MIN_NORMAL; array[r.nextInt(array.length)] = -Float.MAX_VALUE; } }
From source file:Main.java
/** * Fills the array with random doubles. Values will be between min (inclusive) and * max (inclusive).//ww w . j a va2 s . c o m */ public static void genRandomDoubles(long seed, double min, double max, double array[], boolean includeExtremes) { Random r = new Random(seed); int minExponent = Math.min(Math.getExponent(min), 0); int maxExponent = Math.max(Math.getExponent(max), 0); if (minExponent < -6 || maxExponent > 6) { // Use an exponential distribution int exponentDiff = maxExponent - minExponent; for (int i = 0; i < array.length; i++) { double mantissa = r.nextDouble(); int exponent = minExponent + r.nextInt(maxExponent - minExponent); int sign = (min >= 0) ? 1 : 1 - r.nextInt(2) * 2; // -1 or 1 double rand = sign * mantissa * Math.pow(2.0, exponent); if (rand < min || rand > max) { continue; } array[i] = rand; } } else { // Use a linear distribution for (int i = 0; i < array.length; i++) { double rand = r.nextDouble(); array[i] = min + rand * (max - min); } } // Seed a few special numbers we want to be sure to test. for (int i = 0; i < sInterestingDoubles.length; i++) { double d = sInterestingDoubles[i]; if (min <= d && d <= max) { array[r.nextInt(array.length)] = d; } } array[r.nextInt(array.length)] = min; array[r.nextInt(array.length)] = max; if (includeExtremes) { array[r.nextInt(array.length)] = Double.NaN; array[r.nextInt(array.length)] = Double.POSITIVE_INFINITY; array[r.nextInt(array.length)] = Double.NEGATIVE_INFINITY; array[r.nextInt(array.length)] = Double.MIN_VALUE; array[r.nextInt(array.length)] = Double.MIN_NORMAL; array[r.nextInt(array.length)] = Double.MAX_VALUE; array[r.nextInt(array.length)] = -Double.MIN_VALUE; array[r.nextInt(array.length)] = -Double.MIN_NORMAL; array[r.nextInt(array.length)] = -Double.MAX_VALUE; } }
From source file:ml.shifu.shifu.core.binning.DynamicBinningTest.java
public static List<NumBinInfo> createNumBinInfos(int binCnt) { Random rd = new Random(System.currentTimeMillis()); List<Double> thresholds = new ArrayList<Double>(binCnt - 1); for (int i = 0; i < binCnt - 1; i++) { thresholds.add(rd.nextGaussian() * 200); }/*from www. j av a 2s .c o m*/ Collections.sort(thresholds); List<NumBinInfo> binInfoList = NumBinInfo.constructNumBinfo(StringUtils.join(thresholds, ':'), ':'); for (NumBinInfo binInfo : binInfoList) { if (rd.nextDouble() > 0.45) { int total = rd.nextInt() % 1000; int positive = (int) (total * rd.nextDouble()); binInfo.setTotalInstCnt(total); binInfo.setPositiveInstCnt(positive); } } return binInfoList; }
From source file:Main.java
public static void shuffle(final Object[] array, final long seed) { final Random r = new Random(seed); final int limit = array.length; for (int i = 0; i < limit; ++i) { swap(array, i, r.nextInt(limit)); }/* w w w.j ava 2 s . co m*/ }
From source file:io.cortical.retina.model.TestDataHarness.java
/** * Create dummy {@link Fingerprint}./*from w w w .j a va2 s . c om*/ * * @return dummy fingerprint. */ public static Fingerprint createFingerprint() { Random random = new Random(SEED); Set<Integer> positionSet = new LinkedHashSet<>(); while (positionSet.size() <= FINGERPRINT_LENGTH) { positionSet.add(random.nextInt(MAX_POSITION)); } Integer[] positionsInteger = new Integer[FINGERPRINT_LENGTH]; positionsInteger = positionSet.toArray(positionsInteger); sort(positionsInteger); return new Fingerprint(ArrayUtils.toPrimitive(positionsInteger)); }
From source file:Main.java
private static double randomDouble() { Random random = new Random(0); return -5.0 + random.nextDouble() * 10; }
From source file:Util.java
public static long[][] getBinomialDistribution(int min, int max, long total) { Random rand = new Random(System.currentTimeMillis()); int n = max - min; long[][] ret = new long[2][n + 1]; int mean = (n + 1) / 2; float p = 1;//w w w . j a v a 2s . c o m if (n > 0) { p = (float) mean / (float) n; } long count = 0; for (int i = 0; i <= n; i++) { double p_i = combination(n, i) * Math.pow(p, i) * Math.pow((1 - p), (n - i)); long count_i = (long) (total * p_i); ret[0][i] = i + min; ret[1][i] = count_i; count += count_i; } while (count < total) { int i = rand.nextInt(n + 1); ret[1][i]++; count++; } return ret; }
From source file:Util.java
public static long[][] getDiscreteUniformDistribution(int min, int max, long total) { Random rand = new Random(System.currentTimeMillis()); int span = max - min + 1; long avg = total / span; long[][] ret = new long[2][span]; if (avg >= 1) { long count = 0; for (int i = 0; i < span; i++) { ret[0][i] = min + i;/*w ww. ja v a2 s .c o m*/ ret[1][i] = avg; count += avg; } if (count < total) { avg = span / (total - count); int a = (int) avg; int k = 0; while (count < total) { int i = rand.nextInt(span); ret[1][a * k]++; k++; count++; } } } else { for (int i = 0; i < span; i++) { ret[0][i] = min + i; } long count = 0; avg = span / total; while (count < total) { long k = count * avg; int kk = (int) k; ret[1][kk] = 1; count++; // int i = rand.nextInt(span); // if (ret[1][i] == 0) { // ret[1][i] = 1; // count++; // } } } return ret; }