#region License Information /* HeuristicLab * Copyright (C) 2002-2015 Heuristic and Evolutionary Algorithms Laboratory (HEAL) * * This file is part of HeuristicLab. * * HeuristicLab is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * HeuristicLab is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with HeuristicLab. If not, see . */ #endregion using System; using HeuristicLab.Common; using HeuristicLab.Core; using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; namespace HeuristicLab.Random { /// /// Unformliy distributed random variable. /// [Item("UniformDistributedRandom", "A pseudo random number generator to create uniform distributed random numbers.")] [StorableType("6A6E7A7A-4AE2-40FD-9FD1-C821D0DC349D")] public sealed class UniformDistributedRandom : Item, IRandom { [Storable] private double min; /// /// Gets or sets the value for min. /// public double Min { get { return min; } set { min = value; } } [Storable] private double max; /// /// Gets or sets the value for max. /// public double Max { get { return max; } set { max = value; } } [Storable] private IRandom uniform; /// /// Used by HeuristicLab.Persistence to initialize new instances during deserialization. /// /// true, if the constructor is called during deserialization. [StorableConstructor] private UniformDistributedRandom(bool deserializing) : base(deserializing) { } /// /// Initializes a new instance from an existing one (copy constructor). /// /// The original instance which is used to initialize the new instance. /// A which is used to track all already cloned objects in order to avoid cycles. private UniformDistributedRandom(UniformDistributedRandom original, Cloner cloner) : base(original, cloner) { uniform = cloner.Clone(original.uniform); min = original.min; max = original.max; } /// /// Initializes a new instance of with the given parameters. /// /// The random number generator. /// The minimal value (inclusive) /// The maximal value (exclusive). public UniformDistributedRandom(IRandom uniformRandom, double min, double max) { this.min = min; this.max = max; this.uniform = uniformRandom; } #region IRandom Members /// public void Reset() { uniform.Reset(); } /// public void Reset(int seed) { uniform.Reset(seed); } /// /// This method is not implemented. /// public int Next() { throw new NotSupportedException(); } /// /// This method is not implemented. /// public int Next(int maxVal) { throw new NotSupportedException(); } /// /// This method is not implemented. /// public int Next(int minVal, int maxVal) { throw new NotSupportedException(); } /// /// Generates a new double random number. /// /// A double random number. public double NextDouble() { return UniformDistributedRandom.NextDouble(uniform, min, max); } #endregion /// /// Clones the current instance (deep clone). /// /// Deep clone through method of helper class /// . /// Dictionary of all already cloned objects. (Needed to avoid cycles.) /// The cloned object as . public override IDeepCloneable Clone(Cloner cloner) { return new UniformDistributedRandom(this, cloner); } public static double NextDouble(IRandom uniformRandom, double min, double max) { double range = max - min; return uniformRandom.NextDouble() * range + min; } } }