-
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
bb2d8d2
commit 5769631
Showing
6 changed files
with
49 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,39 @@ | ||
package test.support; | ||
|
||
import static org.assertj.core.util.Preconditions.checkArgument; | ||
|
||
import java.util.Objects; | ||
import java.util.Random; | ||
|
||
public final class SafeRandoms { | ||
|
||
private static final Random random = new Random(); | ||
|
||
private SafeRandoms() {} | ||
|
||
/** | ||
* @param delta - Represents a constant that should be added to the generated random value. This | ||
* will ensure that there are no zero values generated. | ||
* @param upperBound - The upper bound (exclusive). Must be positive. | ||
* @return - A random number which is a summation of the delta and the actual random value that is | ||
* lesser than the upperBound value. | ||
*/ | ||
public static int nextInt(int delta, int upperBound) { | ||
return nextInt(delta, upperBound, random); | ||
} | ||
|
||
/** | ||
* @param delta - Represents a constant that should be added to the generated random value. This | ||
* will ensure that there are no zero values generated. | ||
* @param upperBound - The upper bound (exclusive). Must be positive. | ||
* @param random - An existing instance of {@link Random} to be used. | ||
* @return - A random number which is a summation of the delta and the actual random value that is | ||
* lesser than the upperBound value. | ||
*/ | ||
public static int nextInt(int delta, int upperBound, Random random) { | ||
checkArgument(delta >= 0, "Delta should be non-zero"); | ||
checkArgument(upperBound >= 0, "Upper bound should be non-zero"); | ||
checkArgument(delta < upperBound, "Delta should be less than Upper bound"); | ||
return delta + Objects.requireNonNull(random).nextInt(upperBound); | ||
} | ||
} |
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