forked from giacomelli/GeneticSharp
- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFastRandomRandomization.cs
More file actions
Latest commit
85 lines (77 loc) · 2.96 KB
/
Copy pathFastRandomRandomization.cs
File metadata and controls
85 lines (77 loc) · 2.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
usingSystem;
usingSystem.Threading;
usingSharpNeatLib.Maths;
namespaceGeneticSharp
{
/// <summary>
/// An IRandomization using FastRandom has pseudo-number generator.
/// <see href="http://www.codeproject.com/Articles/9187/A-fast-equivalent-for-System-Random"/>
/// </summary>
publicclassFastRandomRandomization:RandomizationBase
{
privatestaticreadonlyFastRandom_globalRandom=newFastRandom(DateTime.Now.Millisecond);
privatestaticreadonlyobject_globalLock=newobject();
privatestaticint?_seed;
/// <summary>
/// Random number generator
/// </summary>
privatestaticThreadLocal<FastRandom>_threadRandom=newThreadLocal<FastRandom>(NewRandom);
/// <summary>
/// Creates a new instance of FastRandom. The seed is derived
/// from a global (static) instance of Random, rather
/// than time.
/// </summary>
privatestaticFastRandomNewRandom()
{
lock(_globalLock)
{
returnnewFastRandom(_seed??_globalRandom.Next(0,int.MaxValue));
}
}
/// <summary>
/// Returns an instance of Random which can be used freely
/// within the current thread.
/// </summary>
privatestaticFastRandomInstance{get{return_threadRandom.Value;}}
/// <summary>
/// Resets the pseudorandom number generator (SharpNeatLib.Maths.FastRandom) initial seed.
/// </summary>
/// <param name="seed">The seed. Use null to reset to default one.</param>
/// <remarks>
/// Can be useful on situation where you need to generate the same result with the same parameter.
/// </remarks>
publicstaticvoidResetSeed(int?seed)
{
_seed=seed;
_threadRandom=newThreadLocal<FastRandom>(NewRandom);
}
/// <summary>
/// Gets an integer value between minimum value (inclusive) and maximum value (exclusive).
/// </summary>
/// <returns>The integer.</returns>
/// <param name="min">Minimum value (inclusive).</param>
/// <param name="max">Maximum value (exclusive).</param>
publicoverrideintGetInt(intmin,intmax)
{
returnInstance.Next(min,max);
}
/// <summary>
/// Gets a float value between 0.0 and 1.0.
/// </summary>
/// <returns>
/// The float value.
/// </returns>
publicoverridefloatGetFloat()
{
return(float)Instance.NextDouble();
}
/// <summary>
/// Gets a double value between 0.0 and 1.0.
/// </summary>
/// <returns>The double value.</returns>
publicoverridedoubleGetDouble()
{
returnInstance.NextDouble();
}
}
}