Example usage for org.apache.commons.math3.random RandomGenerator nextFloat

List of usage examples for org.apache.commons.math3.random RandomGenerator nextFloat

Introduction

In this page you can find the example usage for org.apache.commons.math3.random RandomGenerator nextFloat.

Prototype

float nextFloat();

Source Link

Document

Returns the next pseudorandom, uniformly distributed float value between 0.0 and 1.0 from this random number generator's sequence.

Usage

From source file:com.cloudera.oryx.ml.serving.als.model.LoadTestALSModelFactory.java

private static float[] randomVector(RandomGenerator random) {
    float[] vector = new float[FEATURES];
    for (int i = 0; i < FEATURES; i++) {
        vector[i] = random.nextFloat();
    }/*  w  ww  . ja va 2s  . c om*/
    return vector;
}

From source file:net.openhft.chronicle.timeseries.Columns.java

public static void generateBrownian(DoubleColumn col, double start, double end, double sd) {
    long length = col.length();
    double sd2 = sd / Math.sqrt(length);
    NormalDistribution nd = new NormalDistribution(0, sd2 * CHUNK_SIZE);
    int trendLength = Math.toIntExact((length - 1) / CHUNK_SIZE + 2);
    BytesStore trend = NativeBytesStore.lazyNativeBytesStoreWithFixedCapacity(trendLength * 8L);
    double x = start;
    RandomGenerator rand = new MersenneTwister();
    for (int i = 0; i < trendLength - 1; i++) {
        float f = rand.nextFloat();
        trend.writeDouble((long) i << 3, x);
        x += nd.inverseCumulativeProbability(f);
    }//from w w  w  . j  a v  a2  s .c o m
    trend.writeDouble((long) (trendLength - 1) << 3, x);
    double diff = end - x;
    double gradient = diff / (trendLength - 1);
    for (int i = 0; i < trendLength; i++) {
        double y = trend.addAndGetDoubleNotAtomic((long) i << 3, i * gradient);
        //            System.out.println(i + ": "+y);
    }
    int procs = Runtime.getRuntime().availableProcessors();
    int chunksPerTask = (trendLength - 1) / procs + 1;
    ForkJoinPool fjp = ForkJoinPool.commonPool();
    List<ForkJoinTask> tasks = new ArrayList<>(procs);
    for (int i = 0; i < procs; i++) {
        int si = i * chunksPerTask;
        int ei = Math.min(trendLength, si + chunksPerTask);
        tasks.add(fjp.submit(() -> {
            NormalDistribution nd2 = new NormalDistribution(0, sd2);
            RandomGenerator rand2 = new MersenneTwister();
            for (int j = si; j < ei; j++) {
                generateBrownian(col, (long) j * CHUNK_SIZE, trend.readDouble((long) j << 3),
                        trend.readDouble((long) (j + 1) << 3), nd2, rand2);
            }
        }));
    }
    for (ForkJoinTask task : tasks) {
        task.join();
    }
    trend.release();
}

From source file:net.openhft.chronicle.timeseries.Columns.java

private static void generateBrownian(DoubleColumn col, long first, double start, double end,
        NormalDistribution nd, RandomGenerator rand) {
    double x = start;
    int chunkSize = (int) Math.min(col.length() - first, CHUNK_SIZE);
    for (int i = 0; i < chunkSize; i++) {
        col.set(first + i, x);/*from   w w w .  j a v a  2s . co m*/
        double p = rand.nextFloat() + 0.5 / (1 << 24);
        double v = nd.inverseCumulativeProbability(p);
        x += v;
        assert !Double.isInfinite(x);
    }
    double diff = end - x;
    double gradient = diff / chunkSize;
    for (int i = 0; i < chunkSize; i++) {
        col.add(first + i, i * gradient);
    }
}

From source file:com.github.rinde.rinsim.core.model.rand.RandomModelTest.java

static void testUnmodifiable(RandomGenerator rng) {
    boolean fail = false;
    try {//from  w w w  . j  a  va2 s .  c om
        rng.setSeed(0);
    } catch (final UnsupportedOperationException e) {
        fail = true;
    }
    assertTrue(fail);
    fail = false;
    try {
        rng.setSeed(new int[] { 0 });
    } catch (final UnsupportedOperationException e) {
        fail = true;
    }
    assertTrue(fail);
    fail = false;
    try {
        rng.setSeed(123L);
    } catch (final UnsupportedOperationException e) {
        fail = true;
    }
    assertTrue(fail);
    fail = false;

    rng.nextBoolean();
    rng.nextBytes(new byte[] {});
    rng.nextDouble();
    rng.nextFloat();
    rng.nextGaussian();
    rng.nextInt();
    rng.nextInt(1);
    rng.nextLong();
}