jfaker/src/main/java/com/github/javafaker/service/RandomService.java
Pascal Schumacher bc0129d8e3 regexify should use seed (#96)
update to generex version to 1.0.0 to make this possible
2016-05-05 06:42:14 +10:00

48 lines
1.2 KiB
Java

package com.github.javafaker.service;
import org.apache.commons.lang.math.RandomUtils;
import java.util.Random;
public class RandomService {
private final Random random;
/**
* @param random If null is passed in, a default Random is assigned
*/
public RandomService(Random random) {
this.random = random != null ? random : RandomUtils.JVM_RANDOM;
}
public int nextInt(int n) {
return RandomUtils.nextInt(random, n);
}
public long nextLong() {
return RandomUtils.nextLong(random);
}
// lifted from http://stackoverflow.com/questions/2546078/java-random-long-number-in-0-x-n-range
public long nextLong(long n) {
if (n <= 0) {
throw new IllegalArgumentException("bound must be positive");
}
long bits, val;
do {
long randomLong = RandomUtils.nextLong(random);
bits = (randomLong << 1) >>> 1;
val = bits % n;
} while (bits - val + (n - 1) < 0L);
return val;
}
public double nextDouble() {
return RandomUtils.nextDouble(random);
}
public Boolean nextBoolean() {
return RandomUtils.nextBoolean(random);
}
}