Example usage for java.util.concurrent ThreadLocalRandom current

List of usage examples for java.util.concurrent ThreadLocalRandom current

Introduction

In this page you can find the example usage for java.util.concurrent ThreadLocalRandom current.

Prototype

public static ThreadLocalRandom current() 

Source Link

Document

Returns the current thread's ThreadLocalRandom .

Usage

From source file:nz.co.testamation.testcommon.fixture.SomeFixture.java

public static int someIntLessThan(int maxExclusive) {
    return ThreadLocalRandom.current().nextInt(maxExclusive);
}

From source file:zz.pseas.ghost.utils.LoginUtils.java

public static void sleep(long millis) {
    //Random rand = new Random(System.currentTimeMillis());
    ThreadLocalRandom tlr = ThreadLocalRandom.current();
    // ??30////from ww  w.ja v a2s.  c  o m
    int minInternalTime = 1 * 1000;
    int maxInternalTime = (int) millis;
    int randnum = 0;

    if (maxInternalTime < minInternalTime) {
        maxInternalTime = 3 * minInternalTime + 1;
    }
    randnum = tlr.nextInt(maxInternalTime) % (maxInternalTime - minInternalTime + 1) + minInternalTime;
    //
    if (randnum <= 0)
        return;
    try {
        Thread.sleep(randnum);
        //Thread.currentThread().sleep(randnum);
    } catch (InterruptedException e) {
    }
}

From source file:org.apache.druid.query.aggregation.datasketches.tuple.GenerateTestData.java

private static void generateSketches() throws Exception {
    Path path = FileSystems.getDefault().getPath("array_of_doubles_sketch_data.tsv");
    try (BufferedWriter out = Files.newBufferedWriter(path, StandardCharsets.UTF_8)) {
        Random rand = ThreadLocalRandom.current();
        int key = 0;
        for (int i = 0; i < 20; i++) {
            ArrayOfDoublesUpdatableSketch sketch = new ArrayOfDoublesUpdatableSketchBuilder()
                    .setNominalEntries(1024).build();
            sketch.update(key++, new double[] { 1 });
            sketch.update(key++, new double[] { 1 });
            out.write("2015010101");
            out.write('\t');
            out.write("product_" + (rand.nextInt(10) + 1));
            out.write('\t');
            out.write(Base64.encodeBase64String(sketch.compact().toByteArray()));
            out.newLine();/* w w w .  j  a v a 2 s .co  m*/
        }
    }
}

From source file:org.b3log.solo.util.Images.java

/**
 * Gets an image URL randomly. Sees https://github.com/b3log/bing for more details.
 *
 * @return an image URL//from  w w  w . j  a va 2 s  .c  om
 */
public static final String randImage() {
    try {
        final long min = DateUtils.parseDate("20171104", new String[] { "yyyyMMdd" }).getTime();
        final long max = System.currentTimeMillis();
        final long delta = max - min;
        final long time = ThreadLocalRandom.current().nextLong(0, delta) + min;

        return "https://img.hacpai.com/bing/" + DateFormatUtils.format(time, "yyyyMMdd") + ".jpg";
    } catch (final Exception e) {
        LOGGER.log(Level.ERROR, "Generates random image URL failed", e);

        return "https://img.hacpai.com/bing/20171104.jpg";
    }
}

From source file:org.apache.druid.query.aggregation.datasketches.hll.GenerateTestData.java

private static void generateSketches() throws Exception {
    int lgK = 12;
    String date = "20170101";
    Path rawPath = FileSystems.getDefault().getPath("hll_raw.tsv");
    Path sketchPath = FileSystems.getDefault().getPath("hll_sketches.tsv");
    try (BufferedWriter out1 = Files.newBufferedWriter(rawPath, StandardCharsets.UTF_8)) {
        try (BufferedWriter out2 = Files.newBufferedWriter(sketchPath, StandardCharsets.UTF_8)) {
            Random rand = ThreadLocalRandom.current();
            int key = 0;
            for (int i = 0; i < 100; i++) {
                HllSketch sketch = new HllSketch(lgK);
                String dimension = Integer.toString(rand.nextInt(10) + 1);
                writeRawRecord(out1, date, dimension, key);
                sketch.update(key++);/*from   w  ww. j av a 2  s .c om*/
                writeRawRecord(out1, date, dimension, key);
                sketch.update(key++);
                writeSketchRecord(out2, date, dimension, sketch);
            }
        }
    }
}

From source file:cc.mallet.types.PolyaUrnDirichlet.java

public VSResult nextDistributionWithSparseness(int[] counts) {
    double distribution[] = new double[partition.length];
    TIntArrayList resultingNonZeroIdxs = new TIntArrayList();
    double sum = 0;

    // implements the Poisson Polya Urn
    for (int i = 0; i < distribution.length; i++) {
        // determine whether we land in F_0 or F^_n
        if (counts[i] == 0 || ThreadLocalRandom.current()
                .nextDouble((partition[i] * magnitude) + counts[i]) < partition[i] * magnitude) {
            // sample from F_0
            distribution[i] = (double) nextPoisson(partition[i] * magnitude);
        } else {//from w ww.  j  a v  a 2 s  .  co  m
            // sample from F^_n
            distribution[i] = (double) nextPoisson((double) counts[i]);
        }
        sum += distribution[i];
        if (distribution[i] != 0) {
            resultingNonZeroIdxs.add(i);
        }
    }

    for (int i = 0; i < distribution.length; i++) {
        distribution[i] /= sum;
        if (distribution[i] <= 0) {
            distribution[i] = Double.MIN_VALUE;
        }
    }

    return new VSResult(distribution, resultingNonZeroIdxs.toNativeArray());
}

From source file:edu.javeriana.patronessoftware.rest.DispatcherController.java

@RequestMapping(value = "/agregar", method = RequestMethod.POST)
public ResponseEntity<Frecuencia> agregar(@RequestBody Invernadero invernadero) {
    return new ResponseEntity(new Frecuencia(ThreadLocalRandom.current().nextInt(5, 60)), HttpStatus.OK);
}

From source file:com.salesforce.jprotoc.ProtoTypeUtilsTest.java

@Test
public void classWithEnclosingClass() {
    final String className = randomAlphabetic(ThreadLocalRandom.current().nextInt(5, 10));
    final String enclosingClassName = randomAlphabetic(ThreadLocalRandom.current().nextInt(5, 10));

    assertThat(ProtoTypeMap.toJavaTypeName(className, enclosingClassName, null))
            .isEqualTo(enclosingClassName + "." + className);
}

From source file:com.github.horrorho.inflatabledonkey.cache.StreamCryptor.java

static byte[] randomBytes(int len) {
    byte[] bs = new byte[len];
    ThreadLocalRandom.current().nextBytes(bs);
    return bs;//from   w w  w . java2 s .co  m
}

From source file:io.crate.testing.Utils.java

static int randomAvailablePort(int from, int to) {
    int repeat = 5;
    while (repeat > 0)
        try {/*from w ww .  ja  v  a  2s.c  o m*/
            int port = ThreadLocalRandom.current().nextInt(from, to + 1);
            ServerSocket socket = new ServerSocket(port);
            socket.close();
            return port;
        } catch (IOException ignored) {
            repeat--;
        }
    throw new RuntimeException("no free port found");
}