BAEL-3678 - Generating Random Numbers - Initial commit (#8491)
* BAEL-3678 - Initial commit * BAEL-3678 - Test added * BAEL-3678 - Generated multiple methods and tests * BAEL-3678 - Test improvement
This commit is contained in:
@@ -0,0 +1,91 @@
|
||||
package com.baeldung.randomnumbers;
|
||||
|
||||
import java.security.SecureRandom;
|
||||
import java.util.Random;
|
||||
import java.util.SplittableRandom;
|
||||
import java.util.concurrent.ThreadLocalRandom;
|
||||
import java.util.stream.IntStream;
|
||||
|
||||
import org.apache.commons.math3.random.RandomDataGenerator;
|
||||
|
||||
import it.unimi.dsi.util.XoRoShiRo128PlusRandom;
|
||||
|
||||
public class RandomNumbersGenerator {
|
||||
|
||||
public Integer generateRandomWithMathRandom(int max, int min) {
|
||||
return (int) ((Math.random() * (max - min)) + min);
|
||||
}
|
||||
|
||||
public Integer generateRandomWithNextInt() {
|
||||
Random random = new Random();
|
||||
int randomWithNextInt = random.nextInt();
|
||||
return randomWithNextInt;
|
||||
}
|
||||
|
||||
public Integer generateRandomWithNextIntWithinARange(int min, int max) {
|
||||
Random random = new Random();
|
||||
int randomWintNextIntWithinARange = random.nextInt(max - min) + min;
|
||||
return randomWintNextIntWithinARange;
|
||||
}
|
||||
|
||||
public IntStream generateRandomUnlimitedIntStream() {
|
||||
Random random = new Random();
|
||||
IntStream unlimitedIntStream = random.ints();
|
||||
return unlimitedIntStream;
|
||||
}
|
||||
|
||||
public IntStream generateRandomLimitedIntStream(long streamSize) {
|
||||
Random random = new Random();
|
||||
IntStream limitedIntStream = random.ints(streamSize);
|
||||
return limitedIntStream;
|
||||
}
|
||||
|
||||
public IntStream generateRandomLimitedIntStreamWithinARange(int min, int max, long streamSize) {
|
||||
Random random = new Random();
|
||||
IntStream limitedIntStreamWithinARange = random.ints(streamSize, min, max);
|
||||
return limitedIntStreamWithinARange;
|
||||
}
|
||||
|
||||
public Integer generateRandomWithThreadLocalRandom() {
|
||||
int randomWithThreadLocalRandom = ThreadLocalRandom.current()
|
||||
.nextInt();
|
||||
return randomWithThreadLocalRandom;
|
||||
}
|
||||
|
||||
public Integer generateRandomWithThreadLocalRandomInARange(int min, int max) {
|
||||
int randomWithThreadLocalRandomInARange = ThreadLocalRandom.current()
|
||||
.nextInt(min, max);
|
||||
return randomWithThreadLocalRandomInARange;
|
||||
}
|
||||
|
||||
public Integer generateRandomWithThreadLocalRandomFromZero(int max) {
|
||||
int randomWithThreadLocalRandomFromZero = ThreadLocalRandom.current()
|
||||
.nextInt(max);
|
||||
return randomWithThreadLocalRandomFromZero;
|
||||
}
|
||||
|
||||
public Integer generateRandomWithSplittableRandom(int min, int max) {
|
||||
SplittableRandom splittableRandom = new SplittableRandom();
|
||||
int randomWithSplittableRandom = splittableRandom.nextInt(min, max);
|
||||
return randomWithSplittableRandom;
|
||||
}
|
||||
|
||||
public Integer generateRandomWithSecureRandom() {
|
||||
SecureRandom secureRandom = new SecureRandom();
|
||||
int randomWithSecureRandom = secureRandom.nextInt();
|
||||
return randomWithSecureRandom;
|
||||
}
|
||||
|
||||
public Integer generateRandomWithRandomDataGenerator(int min, int max) {
|
||||
RandomDataGenerator randomDataGenerator = new RandomDataGenerator();
|
||||
int randomWithRandomDataGenerator = randomDataGenerator.nextInt(min, max);
|
||||
return randomWithRandomDataGenerator;
|
||||
}
|
||||
|
||||
public Integer generateRandomWithXoRoShiRo128PlusRandom(int min, int max) {
|
||||
XoRoShiRo128PlusRandom xoroRandom = new XoRoShiRo128PlusRandom();
|
||||
int randomWithXoRoShiRo128PlusRandom = xoroRandom.nextInt(max - min) + min;
|
||||
return randomWithXoRoShiRo128PlusRandom;
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user