| Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
| RandomUtil |
|
| 1.375;1.375 |
| 1 | package nl.beesting.beangenerator.util; | |
| 2 | ||
| 3 | import java.util.Random; | |
| 4 | ||
| 5 | 0 | public class RandomUtil { |
| 6 | 1 | static Random random = new Random(); |
| 7 | ||
| 8 | /** | |
| 9 | * Instantiates random long between given min and max | |
| 10 | * | |
| 11 | * @param min | |
| 12 | * the minimal value (including) | |
| 13 | * @param max | |
| 14 | * the maximal value (excluding) | |
| 15 | * @return | |
| 16 | */ | |
| 17 | public static long initRandomLongBetween(long min, long max) { | |
| 18 | 121980 | long rl = random.nextLong(); |
| 19 | 121980 | if (rl >= min && rl < max) { |
| 20 | 0 | return rl; |
| 21 | } else { | |
| 22 | 121980 | long between = Math.max(max - min, 1); |
| 23 | 121980 | return min + (Math.abs(rl % between)); |
| 24 | } | |
| 25 | } | |
| 26 | ||
| 27 | /** | |
| 28 | * Instantiates random long between given min and max | |
| 29 | * | |
| 30 | * @param min | |
| 31 | * the minimal value | |
| 32 | * @param max | |
| 33 | * the maximal value | |
| 34 | * @return the random long created | |
| 35 | */ | |
| 36 | public static int initRandomIntBetween(int min, int max) { | |
| 37 | 40350 | int ri = random.nextInt(max - min); |
| 38 | 40350 | return ri + min; |
| 39 | } | |
| 40 | ||
| 41 | /** | |
| 42 | * Instantiates random double given min and max | |
| 43 | * | |
| 44 | * @param min | |
| 45 | * the minimal value | |
| 46 | * @param max | |
| 47 | * the maximal value | |
| 48 | * @return the random double created | |
| 49 | */ | |
| 50 | public static double initRandomDoubleBetween(double min, double max) { | |
| 51 | ||
| 52 | 50000 | double randomDouble = Math.random(); |
| 53 | 50000 | double r = ((randomDouble) * (max - min)); |
| 54 | 50000 | return r + min; |
| 55 | } | |
| 56 | ||
| 57 | /** | |
| 58 | * Instantiates random char | |
| 59 | * | |
| 60 | * @todo enable mode generation e.g. alphanumeric / symbolic / etc | |
| 61 | */ | |
| 62 | public static char initRandomChar() { | |
| 63 | 67929 | long positiveLong = initRandomLongBetween((long) Character.MIN_VALUE, (long) Character.MAX_VALUE); |
| 64 | 67929 | char c = (char) positiveLong; |
| 65 | 67929 | return c; |
| 66 | } | |
| 67 | ||
| 68 | /** | |
| 69 | * Method initRandomInt. | |
| 70 | * | |
| 71 | * @return int | |
| 72 | */ | |
| 73 | public static int initRandomInt() { | |
| 74 | 0 | return random.nextInt(); |
| 75 | } | |
| 76 | ||
| 77 | /** | |
| 78 | * Method initRandomLong. | |
| 79 | * | |
| 80 | * @return long | |
| 81 | */ | |
| 82 | public static long initRandomLong() { | |
| 83 | 0 | return random.nextLong(); |
| 84 | } | |
| 85 | ||
| 86 | public static boolean initRandomBoolean() { | |
| 87 | 40 | return random.nextBoolean(); |
| 88 | } | |
| 89 | ||
| 90 | public static void main(String[] args) { | |
| 91 | 0 | for (int i = 0; i < 20; i++) { |
| 92 | 0 | System.out.println(initRandomLongBetween(0, 100)); |
| 93 | } | |
| 94 | 0 | } |
| 95 | } |