#region License Information /* HeuristicLab * Copyright (C) 2002-2013 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 HeuristicLab.Common; using HeuristicLab.Core; using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; namespace HeuristicLab.Problems.Instances.QAPGenerator { // P. Bratley, B.L. Fox, and L.E. Schrage. 1983. A guide to Simulation. Springer, New York. [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.")] [StorableClass] public class Bratley : Item, IRandom { [Storable] private int next; [Storable] private int seed; private const int m = int.MaxValue, a = 16807; public int Next(int minVal, int maxVal) { return (int)(NextDouble() * (maxVal - minVal)) + minVal; } public int Next(int maxVal) { return (int)(NextDouble() * maxVal); } public int Next() { next = Advance(); return next; } public double NextDouble() { return Next() / (double)m; } public void Reset(int seed) { this.seed = seed; this.next = seed; } public void Reset() { this.next = seed; } private int Advance() { return (int)((a * (long)next) % m); } [StorableConstructor] private Bratley(bool deserializing) : base(deserializing) { } private Bratley(Bratley original, Cloner cloner) : base(original, cloner) { this.seed = original.seed; this.next = original.next; } public Bratley() { Reset(new System.Random().Next()); } public Bratley(int seed) { Reset(seed); } public override IDeepCloneable Clone(Cloner cloner) { return new Bratley(this, cloner); } } }