Java Code Examples for org.apache.commons.rng.simple.RandomSource#valueOf()

The following examples show how to use org.apache.commons.rng.simple.RandomSource#valueOf() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ResultsCommand.java    From commons-rng with Apache License 2.0 5 votes vote down vote up
/**
 * Gets the random source from the output header.
 *
 * @param resultFile Result file (for the exception message).
 * @param iter Iterator of the test output.
 * @return the random source
 * @throws ApplicationException If the RandomSource header line cannot be found.
 */
private static RandomSource getRandomSource(File resultFile, Iterator<String> iter) {
    while (iter.hasNext()) {
        final Matcher matcher = RANDOM_SOURCE_PATTERN.matcher(iter.next());
        if (matcher.matches()) {
            return RandomSource.valueOf(matcher.group(1));
        }
    }
    throw new ApplicationException("Failed to find RandomSource header line: " + resultFile);
}
 
Example 2
Source File: DiceGameApplication.java    From commons-rng with Apache License 2.0 5 votes vote down vote up
/**
 * Application's entry point.
 *
 * @param args Arguments, in the following order
 * <ol>
 *  <li>Number of games</li>
 *  <li>Number of players</li>
 *  <li>Number of rounds per game</li>
 *  <li>RNG {@link RandomSource identifier}</li>
 * </ol>
 */
public static void main(String[] args) {
    final int numGames = Integer.parseInt(args[0]);
    final DiceGameApplication app = new DiceGameApplication(Integer.parseInt(args[1]),
                                                            Integer.parseInt(args[2]),
                                                            RandomSource.valueOf(args[3]));

    app.displayModuleInfo();

    for (int i = 1; i <= numGames; i++) {
        System.out.println("--- Game " + i + " ---");
        System.out.println(display(app.game.play()));
    }
}
 
Example 3
Source File: GeometricSamplersPerformance.java    From commons-rng with Apache License 2.0 5 votes vote down vote up
/** Instantiates sampler. */
@Setup
public void setup() {
    final RandomSource randomSource = RandomSource.valueOf(randomSourceName);
    final UniformRandomProvider rng = RandomSource.create(randomSource);
    if ("GeometricSampler".equals(samplerType)) {
        sampler = GeometricSampler.of(rng, probabilityOfSuccess);
    } else {
        final DiscreteInverseCumulativeProbabilityFunction geometricFunction =
            new GeometricDiscreteInverseCumulativeProbabilityFunction(probabilityOfSuccess);
        sampler = InverseTransformDiscreteSampler.of(rng, geometricFunction);
    }
}
 
Example 4
Source File: PoissonSamplerCachePerformance.java    From commons-rng with Apache License 2.0 5 votes vote down vote up
/** Instantiates generator. */
@Setup
public void setup() {
    final RandomSource randomSource = RandomSource
            .valueOf(randomSourceName);
    // Use the same seed
    generator = RandomSource.create(randomSource, SEED.clone());
    state = generator.saveState();
}
 
Example 5
Source File: BaselineSources.java    From commons-rng with Apache License 2.0 5 votes vote down vote up
/** Instantiates generator. This need only be done once per set of iterations. */
@Setup(Level.Trial)
public void setup() {
    if (BASELINE.equals(randomSourceName)) {
        provider = createBaseline();
    } else {
        final RandomSource randomSource = RandomSource.valueOf(randomSourceName);
        provider = RandomSource.create(randomSource);
    }
}
 
Example 6
Source File: ListCommand.java    From commons-rng with Apache License 2.0 4 votes vote down vote up
/**
 * Reads the test data. The {@link Readable} is not closed by this method.
 *
 * @param readable The readable.
 * @return The test data.
 * @throws IOException Signals that an I/O exception has occurred.
 * @throws ApplicationException If there was an error parsing the expected format.
 * @see java.io.Reader#close() Reader.close()
 */
static Iterable<StressTestData> readStressTestData(Readable readable) throws IOException {
    final List<StressTestData> list = new ArrayList<>();

    // Validate that all IDs are unique.
    final HashSet<String> ids = new HashSet<>();

    // Do not use try-with-resources as the readable must not be closed
    @SuppressWarnings("resource")
    final Scanner scanner = new Scanner(readable);
    try {
        while (scanner.hasNextLine()) {
            // Expected format:
            //
            //# ID   RandomSource           trials    [constructor arguments ...]
            //12     TWO_CMRES              1
            //13     TWO_CMRES_SELECT       0         [1, 2]
            final String id = scanner.next();
            // Skip empty lines and comments
            if (id.isEmpty() || id.charAt(0) == '#') {
                scanner.nextLine();
                continue;
            }
            if (!ids.add(id)) {
                throw new ApplicationException("Non-unique ID in strest test data: " + id);
            }
            final RandomSource randomSource = RandomSource.valueOf(scanner.next());
            final int trials = scanner.nextInt();
            // The arguments are the rest of the line
            final String arguments = scanner.nextLine().trim();
            final Object[] args = parseArguments(randomSource, arguments);
            list.add(new StressTestData(id, randomSource, args, trials));
        }
    } catch (NoSuchElementException | IllegalArgumentException ex) {
        if (scanner.ioException() != null) {
            throw scanner.ioException();
        }
        throw new ApplicationException("Failed to read stress test data", ex);
    }

    return list;
}
 
Example 7
Source File: EnumeratedDistributionSamplersPerformance.java    From commons-rng with Apache License 2.0 4 votes vote down vote up
/** Create the random source. */
@Setup
public void setup() {
    final RandomSource randomSource = RandomSource.valueOf(randomSourceName);
    generator = RandomSource.create(randomSource);
}
 
Example 8
Source File: PoissonSamplersPerformance.java    From commons-rng with Apache License 2.0 4 votes vote down vote up
/** Instantiates sampler. */
@Setup
public void setup() {
    final RandomSource randomSource = RandomSource.valueOf(randomSourceName);
    generator = RandomSource.create(randomSource);

    // This would benefit from Java 8 Supplier<DiscreteSampler> lambda function
    if ("SmallMeanPoissonSampler".equals(samplerType)) {
        factory = new DiscreteSamplerFactory() {
            @Override
            public DiscreteSampler create() {
                return SmallMeanPoissonSampler.of(generator, mean);
            }
        };
    } else if ("KempSmallMeanPoissonSampler".equals(samplerType)) {
        factory = new DiscreteSamplerFactory() {
            @Override
            public DiscreteSampler create() {
                return KempSmallMeanPoissonSampler.of(generator, mean);
            }
        };
    } else if ("BoundedKempSmallMeanPoissonSampler".equals(samplerType)) {
        factory = new DiscreteSamplerFactory() {
            @Override
            public DiscreteSampler create() {
                return new BoundedKempSmallMeanPoissonSampler(generator, mean);
            }
        };
    } else if ("KempSmallMeanPoissonSamplerP50".equals(samplerType)) {
        factory = new DiscreteSamplerFactory() {
            @Override
            public DiscreteSampler create() {
                return new KempSmallMeanPoissonSamplerP50(generator, mean);
            }
        };
    } else if ("KempSmallMeanPoissonSamplerBinarySearch".equals(samplerType)) {
        factory = new DiscreteSamplerFactory() {
            @Override
            public DiscreteSampler create() {
                return new KempSmallMeanPoissonSamplerBinarySearch(generator, mean);
            }
        };
    } else if ("KempSmallMeanPoissonSamplerGuideTable".equals(samplerType)) {
        factory = new DiscreteSamplerFactory() {
            @Override
            public DiscreteSampler create() {
                return new KempSmallMeanPoissonSamplerGuideTable(generator, mean);
            }
        };
    } else if ("LargeMeanPoissonSampler".equals(samplerType)) {
        factory = new DiscreteSamplerFactory() {
            @Override
            public DiscreteSampler create() {
                // Note this is not valid when mean < 1
                return LargeMeanPoissonSampler.of(generator, mean);
            }
        };
    } else if ("TinyMeanPoissonSampler".equals(samplerType)) {
        factory = new DiscreteSamplerFactory() {
            @Override
            public DiscreteSampler create() {
                // Note this is only valid when mean < -Math.exp(0x1p-32) == 22.18
                return new TinyMeanPoissonSampler(generator, mean);
            }
        };
    }
    sampler = factory.create();
}
 
Example 9
Source File: DiscreteUniformSamplerGenerationPerformance.java    From commons-rng with Apache License 2.0 4 votes vote down vote up
/** Instantiates generator. */
@Setup
public void setup() {
    final RandomSource randomSource = RandomSource.valueOf(randomSourceName);
    generator = RandomSource.create(randomSource);
}
 
Example 10
Source File: RandomSourceValues.java    From commons-rng with Apache License 2.0 4 votes vote down vote up
/**
 * Look-up the {@link RandomSource} from the name.
 */
@Setup
public void setup() {
    randomSource = RandomSource.valueOf(randomSourceName);
}
 
Example 11
Source File: ThreadLocalPerformance.java    From commons-rng with Apache License 2.0 4 votes vote down vote up
/** Instantiates the random source. */
@Setup
public void setup() {
    randomSource = RandomSource.valueOf(randomSourceName);
}