-
Notifications
You must be signed in to change notification settings - Fork 1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add edit checks to ensure that when using Random it always gives a non zero value
- Loading branch information
1 parent
7180ae4
commit 9fa3167
Showing
6 changed files
with
47 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package test.support; | ||
|
||
import java.util.Objects; | ||
import java.util.Random; | ||
|
||
public final class SafeRandoms { | ||
|
||
private static final Random random = new Random(); | ||
|
||
private SafeRandoms() {} | ||
|
||
/** | ||
* @param bound - The upper bound (exclusive). Must be positive. | ||
* @return - the next pseudorandom, uniformly distributed int value between 1 and bound | ||
* (exclusive) from this random number generator's sequence | ||
*/ | ||
public static int nextInt(int bound) { | ||
return nextInt(random, bound); | ||
} | ||
|
||
/** | ||
* @param random - An existing instance of {@link Random} to be used. | ||
* @param bound - The upper bound (exclusive). Must be positive. | ||
* @return - the next pseudorandom, uniformly distributed int value between 1 and bound | ||
* (exclusive) from this random number generator's sequence | ||
*/ | ||
public static int nextInt(Random random, int bound) { | ||
if (bound <= 0) { | ||
throw new IllegalStateException("The upper bound should be positive number."); | ||
} | ||
int value = 0; | ||
while (value == 0) { | ||
value = Objects.requireNonNull(random).nextInt(bound); | ||
} | ||
return value; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters