Free cookie consent management tool by TermsFeed Policy Generator

source: branches/PersistentDataStructures/HeuristicLab.Random/3.3/UniformDistributedRandom.cs @ 16824

Last change on this file since 16824 was 14186, checked in by swagner, 8 years ago

#2526: Updated year of copyrights in license headers

File size: 4.9 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2016 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
4 *
5 * This file is part of HeuristicLab.
6 *
7 * HeuristicLab is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * HeuristicLab is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
19 */
20#endregion
21
22using System;
23using HeuristicLab.Common;
24using HeuristicLab.Core;
25using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
26
27namespace HeuristicLab.Random {
28
29  /// <summary>
30  /// Unformliy distributed random variable.
31  /// </summary>
32  [Item("UniformDistributedRandom", "A pseudo random number generator to create uniform distributed random numbers.")]
33  [StorableClass]
34  public sealed class UniformDistributedRandom : Item, IRandom {
35    [Storable]
36    private double min;
37    /// <summary>
38    /// Gets or sets the value for min.
39    /// </summary>
40    public double Min {
41      get { return min; }
42      set { min = value; }
43    }
44
45    [Storable]
46    private double max;
47    /// <summary>
48    /// Gets or sets the value for max.
49    /// </summary>
50    public double Max {
51      get { return max; }
52      set { max = value; }
53    }
54
55    [Storable]
56    private IRandom uniform;
57
58    /// <summary>
59    /// Used by HeuristicLab.Persistence to initialize new instances during deserialization.
60    /// </summary>
61    /// <param name="deserializing">true, if the constructor is called during deserialization.</param>
62    [StorableConstructor]
63    private UniformDistributedRandom(bool deserializing) : base(deserializing) { }
64
65    /// <summary>
66    /// Initializes a new instance from an existing one (copy constructor).
67    /// </summary>
68    /// <param name="original">The original <see cref="UniformDistributedRandom"/> instance which is used to initialize the new instance.</param>
69    /// <param name="cloner">A <see cref="Cloner"/> which is used to track all already cloned objects in order to avoid cycles.</param>
70    private UniformDistributedRandom(UniformDistributedRandom original, Cloner cloner)
71      : base(original, cloner) {
72      uniform = cloner.Clone(original.uniform);
73      min = original.min;
74      max = original.max;
75    }
76
77    /// <summary>
78    /// Initializes a new instance of <see cref="UniformDistributedRandom"/> with the given parameters.
79    /// </summary>   
80    /// <param name="uniformRandom">The random number generator.</param>
81    /// <param name="min">The minimal value (inclusive)</param>
82    /// <param name="max">The maximal value (exclusive).</param>
83    public UniformDistributedRandom(IRandom uniformRandom, double min, double max) {
84      this.min = min;
85      this.max = max;
86      this.uniform = uniformRandom;
87    }
88
89    #region IRandom Members
90
91    /// <inheritdoc cref="IRandom.Reset()"/>
92    public void Reset() {
93      uniform.Reset();
94    }
95
96    /// <inheritdoc cref="IRandom.Reset(int)"/>
97    public void Reset(int seed) {
98      uniform.Reset(seed);
99    }
100
101    /// <summary>
102    /// This method is not implemented.
103    /// </summary>
104    public int Next() {
105      throw new NotSupportedException();
106    }
107
108    /// <summary>
109    /// This method is not implemented.
110    /// </summary>
111    public int Next(int maxVal) {
112      throw new NotSupportedException();
113    }
114
115    /// <summary>
116    /// This method is not implemented.
117    /// </summary>
118    public int Next(int minVal, int maxVal) {
119      throw new NotSupportedException();
120    }
121
122    /// <summary>
123    /// Generates a new double random number.
124    /// </summary>
125    /// <returns>A double random number.</returns>
126    public double NextDouble() {
127      return UniformDistributedRandom.NextDouble(uniform, min, max);
128    }
129
130    #endregion
131
132    /// <summary>
133    /// Clones the current instance (deep clone).
134    /// </summary>
135    /// <remarks>Deep clone through <see cref="cloner.Clone"/> method of helper class
136    /// <see cref="Auxiliary"/>.</remarks>
137    /// <param name="clonedObjects">Dictionary of all already cloned objects. (Needed to avoid cycles.)</param>
138    /// <returns>The cloned object as <see cref="UniformDistributedRandom"/>.</returns>
139    public override IDeepCloneable Clone(Cloner cloner) {
140      return new UniformDistributedRandom(this, cloner);
141    }
142
143    public static double NextDouble(IRandom uniformRandom, double min, double max) {
144      double range = max - min;
145      return uniformRandom.NextDouble() * range + min;
146    }
147  }
148}
Note: See TracBrowser for help on using the repository browser.