Skip to content

Commit

Permalink
Merge pull request #10 from patricklaux/dev
Browse files Browse the repository at this point in the history
add RandomUtils
  • Loading branch information
patricklaux committed May 18, 2024
2 parents bbf146f + 940c0c2 commit 06f1d10
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 19 deletions.
24 changes: 12 additions & 12 deletions src/main/java/com/igeeksky/xtool/core/lang/RandomUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -322,31 +322,31 @@ public static double nextDouble() {
/**
* 随机生成 double 值
*
* @param endExclusive endExclusive > 0
* @return 随机 double 值: 0 <= result <= endExclusive
* @param endInclusive endInclusive > 0
* @return 随机 double 值: 0 <= result <= endInclusive
*/
public static double nextDouble(double endExclusive) {
return nextDouble(0, endExclusive);
public static double nextDouble(double endInclusive) {
return nextDouble(0, endInclusive);
}

/**
* 随机生成 double 值
*
* @param startInclusive startInclusive >= 0
* @param endExclusive endExclusive > 0 && endExclusive > startInclusive
* @return 随机 double 值: startInclusive <= result <= endExclusive
* @param endInclusive endInclusive > 0 && endInclusive > startInclusive
* @return 随机 double 值: startInclusive <= result <= endInclusive
*/
public static double nextDouble(double startInclusive, double endExclusive) {
public static double nextDouble(double startInclusive, double endInclusive) {
if (startInclusive < 0) {
throw new IllegalArgumentException("StartInclusive and endExclusive must be non-negative.");
throw new IllegalArgumentException("StartInclusive and endInclusive must be non-negative.");
}
if (startInclusive > endExclusive) {
throw new IllegalArgumentException("endExclusive must be greater than startInclusive.");
if (startInclusive > endInclusive) {
throw new IllegalArgumentException("endInclusive must be greater than startInclusive.");
}
if (endExclusive == startInclusive) {
if (endInclusive == startInclusive) {
return startInclusive;
}
return startInclusive + ((endExclusive - startInclusive) * ThreadLocalRandom.current().nextDouble());
return startInclusive + ((endInclusive - startInclusive) * ThreadLocalRandom.current().nextDouble());
}

/**
Expand Down
14 changes: 7 additions & 7 deletions src/test/java/com/igeeksky/xtool/core/lang/RandomUtilsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -245,10 +245,10 @@ void testNextLong1() {

@Test
public void testNextFloat0() {
float endExclusive = 0;
float endInclusive = 0;
for (int i = 0; i < 1000; i++) {
float v = RandomUtils.nextFloat(endExclusive);
Assertions.assertTrue(v <= endExclusive);
float v = RandomUtils.nextFloat(endInclusive);
Assertions.assertTrue(v <= endInclusive);
}
}

Expand All @@ -275,11 +275,11 @@ public void testNextFloat2() {

@Test
public void nextDouble() {
double endExclusive = 0.0000000000000009D;
double endInclusive = 0.0000000000000009D;
for (int i = 0; i < 1000; i++) {
double v = RandomUtils.nextDouble(endExclusive);
Assertions.assertTrue(v <= endExclusive);
if (v == endExclusive) {
double v = RandomUtils.nextDouble(endInclusive);
Assertions.assertTrue(v <= endInclusive);
if (v == endInclusive) {
System.out.println(v);
}
}
Expand Down

0 comments on commit 06f1d10

Please sign in to comment.