1 /*
   2  * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 package jdk.test.lib.jittester.utils;
  25 
  26 import java.lang.reflect.Field;
  27 import java.util.Collection;
  28 import java.util.Collections;
  29 import java.util.Iterator;
  30 import java.util.List;
  31 import java.util.NoSuchElementException;
  32 import java.util.Random;
  33 import java.util.concurrent.atomic.AtomicLong;
  34 
  35 /**
  36  * This class is used for any random generation operations.
  37  */
  38 public class PseudoRandom {
  39 
  40     private static Random random = null;
  41     private static final Field SEED_FIELD;
  42 
  43     static {
  44         try {
  45             SEED_FIELD = Random.class.getDeclaredField("seed");
  46             SEED_FIELD.setAccessible(true);
  47         } catch (ReflectiveOperationException roe) {
  48             throw new Error("Can't get seed field: " + roe, roe);
  49         }
  50     }
  51 
  52     public static void reset(String seed) {
  53         if (seed == null || seed.length() == 0) {
  54             seed = String.valueOf(System.currentTimeMillis());
  55         }
  56         random = new java.util.Random(seed.hashCode());
  57     }
  58 
  59     public static double random() {
  60         return random.nextDouble();
  61     }
  62 
  63     // uniformly distributed boolean
  64     public static boolean randomBoolean() {
  65         return random.nextBoolean();
  66     }
  67 
  68     // non-uniformly distributed boolean. 0 probability - never true, 1 - always true
  69     public static boolean randomBoolean(double probability) {
  70         return random.nextDouble() < probability;
  71     }
  72 
  73     public static long randomNotZero(long limit) {
  74         long result = (long) (limit * random.nextDouble());
  75         return result > 0L ? result : 1L;
  76     }
  77 
  78     public static int randomNotZero(int limit) {
  79         int result = (int) (limit * random.nextDouble());
  80         return result > 0 ? result : 1;
  81     }
  82 
  83     public static void shuffle(List<?> list) {
  84         Collections.shuffle(list, random);
  85     }
  86 
  87     public static int randomNotNegative(int limit) {
  88         int result = (int) (limit * random.nextDouble());
  89         return Math.abs(result);
  90     }
  91 
  92     public static <T> T randomElement(Collection<T> collection) {
  93         if (collection.isEmpty())
  94             throw new NoSuchElementException("Empty, no element can be randomly selected");
  95         if (collection instanceof List)
  96             return randomElement((List<T>) collection);
  97         else {
  98             int ix = random.nextInt(collection.size());
  99             final Iterator<T> iterator = collection.iterator();
 100             while (ix > 0) {
 101                 ix--;
 102                 iterator.next();
 103             }
 104             return iterator.next();
 105         }
 106     }
 107 
 108     public static <T> T randomElement(List<T> list) {
 109         if (list.isEmpty())
 110             throw new NoSuchElementException("Empty, no element can be randomly selected");
 111         return list.get(random.nextInt(list.size()));
 112     }
 113 
 114     public static <T> T randomElement(T[] array) {
 115         if (array.length == 0)
 116             throw new NoSuchElementException("Empty, no element can be randomly selected");
 117         return array[random.nextInt(array.length)];
 118     }
 119 
 120     public static long getCurrentSeed() {
 121         try {
 122             return ((AtomicLong)SEED_FIELD.get(random)).get();
 123         } catch (ReflectiveOperationException roe) {
 124             throw new Error("Can't get seed: " + roe, roe);
 125         }
 126     }
 127 
 128     public static void setCurrentSeed(long seed) {
 129         try {
 130             AtomicLong seedObject = (AtomicLong)SEED_FIELD.get(random);
 131             seedObject.set(seed);
 132         } catch (ReflectiveOperationException roe) {
 133             throw new Error("Can't set seed: " + roe, roe);
 134         }
 135     }
 136 }