Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2738_QAPGenerators/HeuristicLab.Problems.Instances.QAPGenerator/3.3/PRNG/Bratley.cs @ 17912

Last change on this file since 17912 was 14704, checked in by abeham, 8 years ago

#2738: added branch

File size: 2.5 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2013 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 HeuristicLab.Common;
23using HeuristicLab.Core;
24using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
25
26namespace HeuristicLab.Problems.Instances.QAPGenerator {
27  // P. Bratley, B.L. Fox, and L.E. Schrage. 1983. A guide to Simulation. Springer, New York.
28  [Item("Bratley PRNG", "Generates random numbers according to the recursive formula X_k = (a * X_(k-1)) mod m with a = 16807, m = 2^31 - 1 and X_0 being the seed.")]
29  [StorableClass]
30  public class Bratley : Item, IRandom {
31    [Storable]
32    private int next;
33    [Storable]
34    private int seed;
35
36    private const int m = int.MaxValue, a = 16807;
37
38    public int Next(int minVal, int maxVal) {
39      return (int)(NextDouble() * (maxVal - minVal)) + minVal;
40    }
41
42    public int Next(int maxVal) {
43      return (int)(NextDouble() * maxVal);
44    }
45
46    public int Next() {
47      next = Advance();
48      return next;
49    }
50
51    public double NextDouble() {
52      return Next() / (double)m;
53    }
54
55    public void Reset(int seed) {
56      this.seed = seed;
57      this.next = seed;
58    }
59
60    public void Reset() {
61      this.next = seed;
62    }
63
64    private int Advance() {
65      return (int)((a * (long)next) % m);
66    }
67
68    [StorableConstructor]
69    private Bratley(bool deserializing) : base(deserializing) { }
70    private Bratley(Bratley original, Cloner cloner)
71      : base(original, cloner) {
72      this.seed = original.seed;
73      this.next = original.next;
74    }
75    public Bratley() {
76      Reset(new System.Random().Next());
77    }
78    public Bratley(int seed) {
79      Reset(seed);
80    }
81
82    public override IDeepCloneable Clone(Cloner cloner) {
83      return new Bratley(this, cloner);
84    }
85  }
86}
Note: See TracBrowser for help on using the repository browser.