001/* 002 * Java Genetic Algorithm Library (jenetics-8.1.0). 003 * Copyright (c) 2007-2024 Franz Wilhelmstötter 004 * 005 * Licensed under the Apache License, Version 2.0 (the "License"); 006 * you may not use this file except in compliance with the License. 007 * You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 * 017 * Author: 018 * Franz Wilhelmstötter (franz.wilhelmstoetter@gmail.com) 019 */ 020package io.jenetics.stat; 021 022import java.util.random.RandomGenerator; 023 024import io.jenetics.util.DoubleRange; 025import io.jenetics.util.IntRange; 026 027/** 028 * Interface for creating <em>continuous</em> random samples, with a given 029 * <em>distribution</em>. This interface isn't responsible for creating the 030 * random numbers itself. It uses a {@link RandomGenerator} generator, which is 031 * given by the caller. 032 * {@snippet lang = java: 033 * final var random = RandomGenerator.getDefault(); 034 * final var range = DoubleRange.of(0, 1); 035 * final var sampler = Sampler.linear(0.1); 036 * // Create a new sample point, which obeys the given distribution. 037 * // The random generator is responsible for the base randomness. 038 * final double value = sampler.sample(random, range); 039 *} 040 * 041 * @see Samplers 042 * @see <a href="https://en.wikipedia.org/wiki/Inverse_transform_sampling"> 043 * Inverse transform sampling</a> 044 * 045 * @author <a href="mailto:franz.wilhelmstoetter@gmail.com">Franz Wilhelmstötter</a> 046 * @version 8.0 047 * @since 8.0 048 */ 049@FunctionalInterface 050public interface Sampler { 051 052 /** 053 * Default uniform distribution by calling 054 * {@link RandomGenerator#nextDouble(double, double)}. 055 */ 056 Sampler UNIFORM = (random, range) -> random.nextDouble(range.min(), range.max()); 057 058 /** 059 * Create a new {@code double} sample point within the given range 060 * {@code [min, max)}. 061 * 062 * @param random the random generator used for creating a sample point of 063 * the defined distribution 064 * @param range the range of the sample point: {@code [min, max)} 065 * @return a new sample point between {@code [min, max)} 066 */ 067 double sample(RandomGenerator random, DoubleRange range); 068 069 /** 070 * Create a new {@code int} sample point within the given range 071 * {@code [min, max)}. 072 * 073 * @param random the random generator used for creating a sample point of 074 * the defined distribution 075 * @param range the range of the sample point: {@code [min, max)} 076 * @return a new sample point between {@code [min, max)} 077 */ 078 default int sample(RandomGenerator random, IntRange range) { 079 return (int)sample(random, DoubleRange.of(range.min(), range.max())); 080 } 081 082}