Example usage for java.util.concurrent ThreadLocalRandom nextInt

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

Introduction

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

Prototype

public int nextInt(int origin, int bound) 

Source Link

Document

Returns a pseudorandom int value between the specified origin (inclusive) and the specified bound (exclusive).

Usage

From source file:fi.luontola.cqrshotel.JsonSerializationTest.java

private static Object randomValue(Class<?> type) {
    ThreadLocalRandom random = ThreadLocalRandom.current();
    if (type == UUID.class) {
        return UUID.randomUUID();
    }/*from   w ww  .  j a  v a 2 s. c  om*/
    if (type == LocalDate.class) {
        return LocalDate.of(random.nextInt(2000, 2100),
                random.nextInt(Month.JANUARY.getValue(), Month.DECEMBER.getValue() + 1),
                random.nextInt(1, Month.FEBRUARY.minLength() + 1));
    }
    if (type == Money.class) {
        return Money.of(random.nextDouble(0, 1000), pickRandom(Monetary.getCurrencies()));
    }
    if (type == Instant.class) {
        return Instant.ofEpochMilli(random.nextLong());
    }
    if (type == String.class) {
        return RandomStringUtils.randomAlphanumeric(random.nextInt(10));
    }
    if (type == int.class) {
        return random.nextInt();
    }
    throw new IllegalArgumentException("Unsupported type: " + type);
}

From source file:com.toptal.conf.SampleDataConfiguration.java

/**
 * Adds entries to the given user./* www. j a va  2s. co  m*/
 * @param user User.
 */
@SuppressWarnings("PMD.AvoidInstantiatingObjectsInLoops")
private void addEntries(final User user) {
    final ThreadLocalRandom rnd = ThreadLocalRandom.current();
    final int size = rnd.nextInt(10, 100);
    final Calendar cal = Calendar.getInstance();
    final long end = cal.getTimeInMillis();
    cal.set(Calendar.MONTH, cal.get(Calendar.MONTH) - 2);
    final long start = cal.getTimeInMillis();
    for (int idx = 0; idx < size; ++idx) {
        this.entries.save(Entry.builder().date(new Date(rnd.nextLong(start, end)))
                // @checkstyle MagicNumberCheck (2 lines)
                .distance(rnd.nextLong(500, 10000)).time(rnd.nextLong(120000, 2400000)).user(user).build());
    }
}

From source file:com.haulmont.cuba.core.UniqueNumbersTest.java

@Test
public void testConcurrentModification() throws Exception {
    int threadCnt = 8;
    ExecutorService executorService = Executors.newFixedThreadPool(threadCnt,
            new ThreadFactoryBuilder().setNameFormat("T%d").build());

    final Action[] actions = { new SleepAction(), new GetNumberAction(), new SetNumberAction(),
            new DeleteSequenceAction() };

    for (int i = 0; i < threadCnt; i++) {
        final int finalI = i;
        executorService.submit(new Runnable() {

            int runnableNo = finalI;

            @Override/*  www  . java  2 s  . c o m*/
            public void run() {
                ThreadLocalRandom random = ThreadLocalRandom.current();
                for (int i = 0; i < 10; i++) {
                    int action = random.nextInt(0, 4);
                    System.out.println(
                            "Runnable " + runnableNo + " iteration " + i + " action " + actions[action]);
                    try {
                        int seqN = actions[action].perform(runnableNo);
                        actions[action].success(runnableNo, seqN);
                    } catch (Exception e) {
                        if (e instanceof IllegalStateException
                                && StringUtils.contains(e.getMessage(), "Attempt to delete")) {
                            System.err.println(e.getMessage());
                            continue;
                        }
                        System.err.println(e.getMessage());
                        exceptionCnt.incrementAndGet();
                    }
                }
                finishedThreads.incrementAndGet();
            }
        });
    }

    while (finishedThreads.get() < threadCnt) {
        System.out.println("Waiting...");
        TimeUnit.MILLISECONDS.sleep(200);
    }

    assertEquals(exceptionCnt.get(), 0);
}