Skip to content
This repository has been archived by the owner on Apr 14, 2023. It is now read-only.

Commit

Permalink
Merge pull request #1519 from finos/1517-random-upperbound-functionality
Browse files Browse the repository at this point in the history
1517 random upperbound functionality
  • Loading branch information
r-stuart authored Nov 7, 2019
2 parents 63fa581 + a9a969b commit f8b7fe7
Show file tree
Hide file tree
Showing 7 changed files with 86 additions and 65 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ public OffsetDateTime getPrevious(OffsetDateTime value) {

@Override
public OffsetDateTime getRandom(OffsetDateTime min, OffsetDateTime max, RandomNumberGenerator randomNumberGenerator) {
long generatedLong = (long) randomNumberGenerator.nextDouble(getMilli(min), getMilli(max));
long generatedLong = randomNumberGenerator.nextLong(getMilli(min), getMilli(max));

OffsetDateTime generatedDate = Instant.ofEpochMilli(generatedLong).atZone(ZoneOffset.UTC).toOffsetDateTime();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
public interface RandomNumberGenerator {
int nextInt();
int nextInt(int bound);
int nextInt(int lowerInclusive, int upperExclusive);
long nextLong(long lowerInclusive, long upperInclusive);
double nextDouble(double lowerInclusive, double upperExclusive);
BigDecimal nextBigDecimal(BigDecimal lowerInclusive, BigDecimal upperExclusive);
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,9 @@ public int nextInt(int bound) {
}

@Override
public int nextInt(int lowerInclusive, int upperExclusive) {
return 0;
public long nextLong(long lowerInclusive, long upperExclusive) {
long multiplier = (long) (nextDoubleValue * (double) (upperExclusive - lowerInclusive));
return multiplier + lowerInclusive;
}

@Override
Expand All @@ -50,6 +51,6 @@ public double nextDouble(double lower, double upper) {

@Override
public BigDecimal nextBigDecimal(BigDecimal lowerInclusive, BigDecimal upperExclusive) {
return new BigDecimal(nextDouble(lowerInclusive.doubleValue(), upperExclusive.doubleValue()));
return BigDecimal.valueOf(nextDouble(lowerInclusive.doubleValue(), upperExclusive.doubleValue()));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,10 @@ public Stream<T> generateInterestingValues() {
@Override
public Stream<T> generateRandomValues(RandomNumberGenerator randomNumberGenerator) {
return Stream.generate(() -> restrictions.getGranularity()
.getRandom(restrictions.getMin(), restrictions.getMax(), randomNumberGenerator))
.getRandom(
restrictions.getMin(),
restrictions.getGranularity().getNext(restrictions.getMax()),
randomNumberGenerator))
.filter(this::notInBlacklist);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
package com.scottlogic.deg.generator.utils;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.Random;

public class JavaUtilRandomNumberGenerator implements RandomNumberGenerator {
Expand All @@ -42,37 +41,23 @@ public int nextInt(int bound) {
}

@Override
public int nextInt(int lowerInclusive, int upperExclusive) {
// implementation copied from Random::internalNextInt
if (lowerInclusive < upperExclusive) {
int n = upperExclusive - lowerInclusive;
if (n > 0) {
return nextInt(n) + lowerInclusive;
}
else { // range not representable as int
int r;
do {
r = nextInt();
} while (r < lowerInclusive || r >= upperExclusive);
return r;
}
}
else {
return nextInt();
}
public long nextLong(long lowerInclusive, long upperExclusive) {
long multiplier = (long) (random.nextDouble() * (double) (upperExclusive - lowerInclusive));
return multiplier + lowerInclusive;
}

@Override
public double nextDouble(double lowerInclusive, double upperExclusive) {
return random.nextDouble()
* (upperExclusive - lowerInclusive)
return (random.nextDouble()
* (upperExclusive - lowerInclusive))
+ lowerInclusive;
}

@Override
public BigDecimal nextBigDecimal(BigDecimal lowerInclusive, BigDecimal upperExclusive) {
return new BigDecimal(random.nextDouble())
return BigDecimal.valueOf(random.nextDouble())
.multiply(upperExclusive.subtract(lowerInclusive))
.add(lowerInclusive);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package com.scottlogic.deg.generator.generation.fieldvaluesources;

import com.scottlogic.deg.common.profile.NumericGranularity;
import com.scottlogic.deg.generator.restrictions.linear.LinearRestrictions;
import com.scottlogic.deg.generator.utils.JavaUtilRandomNumberGenerator;
import org.junit.jupiter.api.Test;

import java.math.BigDecimal;
import java.util.Collections;
import java.util.stream.Stream;

import static org.junit.jupiter.api.Assertions.*;

class LinearFieldValueSourceTest {

@Test
public void testGenerateRandomValues() {
LinearRestrictions<BigDecimal> restrictions = new LinearRestrictions<>(
BigDecimal.ZERO,
BigDecimal.valueOf(4),
new NumericGranularity(0));
LinearFieldValueSource<BigDecimal> source = new LinearFieldValueSource<>(restrictions, Collections.emptySet());

Stream<BigDecimal> results = source.generateRandomValues(new JavaUtilRandomNumberGenerator());

assertTrue(results.limit(100).allMatch(x -> x.intValue() >= 0 & x.intValue() < 5));
}

}
Loading

0 comments on commit f8b7fe7

Please sign in to comment.