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.util.Collection;
  27 import java.util.Collections;
  28 import java.util.Iterator;
  29 import java.util.List;
  30 import java.util.NoSuchElementException;
  31 
  32 /**
  33  * This class is used for any random generation operations.
  34  */
  35 public class PseudoRandom {
  36 
  37     private static java.util.Random random = null;
  38 
  39     public static void reset(String seed) {
  40         if (seed == null || seed.length() == 0) {
  41             seed = String.valueOf(System.currentTimeMillis());
  42         }
  43         random = new java.util.Random(seed.hashCode());
  44     }
  45 
  46     public static double random() {
  47         return random.nextDouble();
  48     }
  49 
  50     // uniformly distributed boolean
  51     public static boolean randomBoolean() {
  52         return random.nextBoolean();
  53     }
  54 
  55     // non-uniformly distributed boolean. 0 probability - never true, 1 - always true
  56     public static boolean randomBoolean(double probability) {
  57         return random.nextDouble() < probability;
  58     }
  59 
  60     public static long randomNotZero(long limit) {
  61         long result = (long) (limit * random.nextDouble());
  62         return result > 0L ? result : 1L;
  63     }
  64 
  65     public static int randomNotZero(int limit) {
  66         int result = (int) (limit * random.nextDouble());
  67         return result > 0 ? result : 1;
  68     }
  69 
  70     public static void shuffle(List<?> list) {
  71         Collections.shuffle(list, random);
  72     }
  73 
  74     public static byte randomNotNegative(byte limit) {
  75         byte result = (byte) (limit * random.nextDouble());
  76         return (byte)Math.abs(result);
  77     }
  78 
  79     public static <T> T randomElement(Collection<T> collection) {
  80         if (collection.isEmpty())
  81             throw new NoSuchElementException("Empty, no element can be randomly selected");
  82         if (collection instanceof List)
  83             return randomElement((List<T>) collection);
  84         else {
  85             int ix = random.nextInt(collection.size());
  86             final Iterator<T> iterator = collection.iterator();
  87             while (ix > 0) {
  88                 ix--;
  89                 iterator.next();
  90             }
  91             return iterator.next();
  92         }
  93     }
  94 
  95     public static <T> T randomElement(List<T> list) {
  96         if (list.isEmpty())
  97             throw new NoSuchElementException("Empty, no element can be randomly selected");
  98         return list.get(random.nextInt(list.size()));
  99     }
 100 
 101     public static <T> T randomElement(T[] array) {
 102         if (array.length == 0)
 103             throw new NoSuchElementException("Empty, no element can be randomly selected");
 104         return array[random.nextInt(array.length)];
 105     }
 106 }