/* * Encog(tm) Core v3.4 - Java Version * http://www.heatonresearch.com/encog/ * https://github.com/encog/encog-java-core * Copyright 2008-2016 Heaton Research, Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. * * For more information on Heaton Research copyrights, licenses * and trademarks visit: * http://www.heatonresearch.com/copyright */ package org.encog.mathutil.randomize; import java.util.Random; /** * A randomizer that will create random weight and bias values that are between * a specified range. * * @author jheaton * */ public class RangeRandomizer extends BasicRandomizer { /** * Returns a random number in the range between min and max. * @param min The minimum desired random number. * @param max The maximum desired random number. * @return The random number. */ public static int randomInt(final int min, final int max) { return (int) RangeRandomizer.randomize(min, max + 1); } /** * Generate a random number in the specified range. * * @param min * The minimum value. * @param max * The maximum value. * @return A random number. */ public static double randomize(final double min, final double max) { final double range = max - min; return (range * Math.random()) + min; } public static double randomize(final Random r, final double min, final double max) { final double range = max - min; return (range * r.nextDouble()) + min; } /** * The minimum value for the random range. */ private final double min; /** * The maximum value for the random range. */ private final double max; /** * Construct a range randomizer. * * @param min * The minimum random value. * @param max * The maximum random value. */ public RangeRandomizer(final double min, final double max) { this.max = max; this.min = min; } /** * Generate a random number based on the range specified in the constructor. * * @param d * The range randomizer ignores this value. * @return The random number. */ public double randomize(final double d) { return nextDouble(this.min, this.max); } /** * @return the min */ public double getMin() { return min; } /** * @return the max */ public double getMax() { return max; } }