Skip to content

Commit 8530388

Browse files
aitorcuestamaibin
authored andcommitted
BAEL-3678 - Moving from java-numbers-2 to java-numbers-3 (eugenp#8521)
1 parent 23874fb commit 8530388

4 files changed

Lines changed: 40 additions & 8 deletions

File tree

java-numbers-2/pom.xml

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,6 @@
2121
<artifactId>jmh-generator-annprocess</artifactId>
2222
<version>${jmh-generator.version}</version>
2323
</dependency>
24-
<dependency>
25-
<groupId>it.unimi.dsi</groupId>
26-
<artifactId>dsiutils</artifactId>
27-
<version>2.6.0</version>
28-
</dependency>
2924
</dependencies>
3025

3126
<build>

java-numbers-3/pom.xml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,14 @@
1212
<version>0.0.1-SNAPSHOT</version>
1313
<relativePath>../parent-java</relativePath>
1414
</parent>
15+
16+
<dependencies>
17+
<dependency>
18+
<groupId>it.unimi.dsi</groupId>
19+
<artifactId>dsiutils</artifactId>
20+
<version>2.6.0</version>
21+
</dependency>
22+
</dependencies>
1523

1624
<build>
1725
<finalName>java-numbers-3</finalName>

java-numbers-2/src/main/java/com/baeldung/randomnumbers/RandomNumbersGenerator.java renamed to java-numbers-3/src/main/java/com/baeldung/randomnumbers/RandomNumbersGenerator.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,12 +70,24 @@ public Integer generateRandomWithSplittableRandom(int min, int max) {
7070
return randomWithSplittableRandom;
7171
}
7272

73+
public IntStream generateRandomWithSplittableRandomLimitedIntStreamWithinARange(int min, int max, long streamSize) {
74+
SplittableRandom splittableRandom = new SplittableRandom();
75+
IntStream limitedIntStreamWithinARangeWithSplittableRandom = splittableRandom.ints(streamSize, min, max);
76+
return limitedIntStreamWithinARangeWithSplittableRandom;
77+
}
78+
7379
public Integer generateRandomWithSecureRandom() {
7480
SecureRandom secureRandom = new SecureRandom();
7581
int randomWithSecureRandom = secureRandom.nextInt();
7682
return randomWithSecureRandom;
7783
}
7884

85+
public Integer generateRandomWithSecureRandomWithinARange(int min, int max) {
86+
SecureRandom secureRandom = new SecureRandom();
87+
int randomWithSecureRandomWithinARange = secureRandom.nextInt(max - min) + min;
88+
return randomWithSecureRandomWithinARange;
89+
}
90+
7991
public Integer generateRandomWithRandomDataGenerator(int min, int max) {
8092
RandomDataGenerator randomDataGenerator = new RandomDataGenerator();
8193
int randomWithRandomDataGenerator = randomDataGenerator.nextInt(min, max);

java-numbers-2/src/test/java/com/baeldung/randomnumbers/RandomNumbersGeneratorUnitTest.java renamed to java-numbers-3/src/test/java/com/baeldung/randomnumbers/RandomNumbersGeneratorUnitTest.java

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ public class RandomNumbersGeneratorUnitTest {
1111

1212
private static final int MIN_RANGE = 1;
1313
private static final int MAX_RANGE = 10;
14+
private static final int MIN_RANGE_NEGATIVE = -10;
1415
private static final int ITERATIONS = 50;
1516
private static final long STREAM_SIZE = 50;
1617

@@ -19,7 +20,7 @@ public void whenGenerateRandomWithMathRandom_returnsSuccessfully() {
1920
RandomNumbersGenerator generator = new RandomNumbersGenerator();
2021
for (int i = 0; i < ITERATIONS; i++) {
2122
int randomNumer = generator.generateRandomWithMathRandom(MIN_RANGE, MAX_RANGE);
22-
assertTrue(isInRange(randomNumer, Integer.MIN_VALUE, Integer.MAX_VALUE));
23+
assertTrue(isInRange(randomNumer, MIN_RANGE, MAX_RANGE));
2324
}
2425
}
2526

@@ -97,10 +98,17 @@ public void whenGenerateRandomWithThreadLocalRandomFromZero_returnsSuccessfully(
9798
public void whenGenerateRandomWithSplittableRandom_returnsSuccessfully() {
9899
RandomNumbersGenerator generator = new RandomNumbersGenerator();
99100
for (int i = 0; i < ITERATIONS; i++) {
100-
int randomNumber = generator.generateRandomWithSplittableRandom(MIN_RANGE, MAX_RANGE);
101-
assertTrue(isInRange(randomNumber, MIN_RANGE, MAX_RANGE));
101+
int randomNumber = generator.generateRandomWithSplittableRandom(MIN_RANGE_NEGATIVE, MAX_RANGE);
102+
assertTrue(isInRange(randomNumber, MIN_RANGE_NEGATIVE, MAX_RANGE));
102103
}
103104
}
105+
106+
@Test
107+
public void whenGenerateRandomWithSplittableRandomLimitedIntStreamWithinARange_returnsSuccessfully() {
108+
RandomNumbersGenerator generator = new RandomNumbersGenerator();
109+
generator.generateRandomWithSplittableRandomLimitedIntStreamWithinARange(MIN_RANGE, MAX_RANGE, STREAM_SIZE)
110+
.forEach(randomNumber -> assertTrue(isInRange(randomNumber, MIN_RANGE, MAX_RANGE)));
111+
}
104112

105113
@Test
106114
public void whenGenerateRandomWithSecureRandom_returnsSuccessfully() {
@@ -110,6 +118,15 @@ public void whenGenerateRandomWithSecureRandom_returnsSuccessfully() {
110118
assertTrue(isInRange(randomNumber, Integer.MIN_VALUE, Integer.MAX_VALUE));
111119
}
112120
}
121+
122+
@Test
123+
public void whenGenerateRandomWithSecureRandomWithinARange_returnsSuccessfully() {
124+
RandomNumbersGenerator generator = new RandomNumbersGenerator();
125+
for (int i = 0; i < ITERATIONS; i++) {
126+
int randomNumber = generator.generateRandomWithSecureRandomWithinARange(MIN_RANGE, MAX_RANGE);
127+
assertTrue(isInRange(randomNumber, MIN_RANGE, MAX_RANGE));
128+
}
129+
}
113130

114131
@Test
115132
public void whenGenerateRandomWithRandomDataGenerator_returnsSuccessfully() {

0 commit comments

Comments
 (0)