public static int between(int min, int max) { return RandomNumbers.randomIntBetween(RandomizedContext.current().getRandom(), min, max); }
/** * Shortcut for {@link RandomizedContext#getRandom()}. Even though this method * is static, it returns per-thread {@link Random} instance, so no race conditions * can occur. * * <p>It is recommended that specific methods are used to pick random values. */ public static Random getRandom() { return getContext().getRandom(); }
/** * Shortcut for {@link RandomizedContext#getRandom()}. Even though this method * is static, it returns per-thread {@link Random} instance, so no race conditions * can occur. * * <p>It is recommended that specific methods are used to pick random values. */ public static Random getRandom() { return getContext().getRandom(); }
@Test public void getContextByHand() { RandomizedContext context = RandomizedContext.current(); Random rnd = context.getRandom(); System.out.println("Random, next int: " + rnd.nextInt()); }
/** * Access to the current {@link RandomizedContext}'s Random instance. It is safe to use * this method from multiple threads, etc., but it should be called while within a runner's * scope (so no static initializers). The returned {@link Random} instance will be * <b>different</b> when this method is called inside a {@link BeforeClass} hook (static * suite scope) and within {@link Before}/ {@link After} hooks or test methods. * * <p>The returned instance must not be shared with other threads or cross a single scope's * boundary. For example, a {@link Random} acquired within a test method shouldn't be reused * for another test case. * * <p>There is an overhead connected with getting the {@link Random} for a particular context * and thread. It is better to cache the {@link Random} locally if tight loops with multiple * invocations are present or create a derivative local {@link Random} for millions of calls * like this: * <pre> * Random random = new Random(random().nextLong()); * // tight loop with many invocations. * </pre> */ public static Random random() { return RandomizedContext.current().getRandom(); }
public static int between(int min, int max) { return RandomNumbers.randomIntBetween(RandomizedContext.current().getRandom(), min, max); }
/** * Access to the current {@link com.carrotsearch.randomizedtesting.RandomizedContext}'s Random instance. It is safe to use * this method from multiple threads, etc., but it should be called while within a runner's * scope (so no static initializers). The returned {@link java.util.Random} instance will be * <b>different</b> when this method is called inside a {@link org.junit.BeforeClass} hook (static * suite scope) and within {@link org.junit.Before}/ {@link org.junit.After} hooks or test methods. * <p/> * <p>The returned instance must not be shared with other threads or cross a single scope's * boundary. For example, a {@link java.util.Random} acquired within a test method shouldn't be reused * for another test case. * <p/> * <p>There is an overhead connected with getting the {@link java.util.Random} for a particular context * and thread. It is better to cache the {@link java.util.Random} locally if tight loops with multiple * invocations are present or create a derivative local {@link java.util.Random} for millions of calls * like this: * <pre> * Random random = new Random(random().nextLong()); * // tight loop with many invocations. * </pre> */ public static Random random() { return RandomizedContext.current().getRandom(); }