Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Random/3.3/FastRandomCreator.cs @ 4243

Last change on this file since 4243 was 4243, checked in by gkronber, 14 years ago

Added a memory efficient PRNG (FastRandom) and a specific operator to create new FastRandom PRNGs. #1114

File size: 3.1 KB
RevLine 
[4243]1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2010 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.Core;
23using HeuristicLab.Data;
24using HeuristicLab.Operators;
25using HeuristicLab.Parameters;
26using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
27
28namespace HeuristicLab.Random {
29  /// <summary>
30  /// An operator which creates a new "FastRandom" pseudo random number generator.
31  /// </summary>
32  [Item("FastRandomCreator", "An operator which creates a new \'FastRandom\' pseudo random number generator.")]
33  [StorableClass]
34  public sealed class FastRandomCreator : SingleSuccessorOperator {
35    public ValueLookupParameter<BoolValue> SetSeedRandomlyParameter {
36      get { return (ValueLookupParameter<BoolValue>)Parameters["SetSeedRandomly"]; }
37    }
38    public ValueLookupParameter<IntValue> SeedParameter {
39      get { return (ValueLookupParameter<IntValue>)Parameters["Seed"]; }
40    }
41    public LookupParameter<IRandom> RandomParameter {
42      get { return (LookupParameter<IRandom>)Parameters["Random"]; }
43    }
44    public BoolValue SetSeedRandomly {
45      get { return SetSeedRandomlyParameter.Value; }
46      set { SetSeedRandomlyParameter.Value = value; }
47    }
48    public IntValue Seed {
49      get { return SeedParameter.Value; }
50      set { SeedParameter.Value = value; }
51    }
52
53    public FastRandomCreator()
54      : base() {
55      Parameters.Add(new ValueLookupParameter<BoolValue>("SetSeedRandomly", "True if the random seed should be set to a random value, otherwise false.", new BoolValue(true)));
56      Parameters.Add(new ValueLookupParameter<IntValue>("Seed", "The random seed used to initialize the new pseudo random number generator.", new IntValue(0)));
57      Parameters.Add(new LookupParameter<IRandom>("Random", "The new pseudo random number generator which is initialized with the given seed."));
58    }
59
60    public override IOperation Apply() {
61      if (SetSeedRandomlyParameter.ActualValue == null) SetSeedRandomlyParameter.ActualValue = new BoolValue(true);
62      bool setSeedRandomly = SetSeedRandomlyParameter.ActualValue.Value;
63      if (SeedParameter.ActualValue == null) SeedParameter.ActualValue = new IntValue(0);
64      IntValue seed = SeedParameter.ActualValue;
65
66      if (setSeedRandomly) seed.Value = new System.Random().Next();
67      RandomParameter.ActualValue = new FastRandom(seed.Value);
68
69      return base.Apply();
70    }
71  }
72}
Note: See TracBrowser for help on using the repository browser.