forked from giacomelli/GeneticSharp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBasicRandomization.cs
68 lines (61 loc) · 1.99 KB
/
BasicRandomization.cs
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
using System;
using System.Threading;
namespace GeneticSharp.Domain.Randomizations
{
/// <summary>
/// An IRandomization implementation using System.Random has pseudo-number generator.
/// </summary>
public class BasicRandomization : RandomizationBase
{
#region Fields
// TODO: change to ThreadLocal when we migrate GeneticSharp to .NET 4.0+.
// http://codeblog.jonskeet.uk/2009/11/04/revisiting-randomness/
private static int s_seed = Environment.TickCount;
[ThreadStatic]
private static Random s_random;
#endregion
#region Properties
private static Random Random
{
get
{
if (s_random == null)
{
s_random = new Random(Interlocked.Increment(ref s_seed));
}
return s_random;
}
}
#endregion
#region Methods
/// <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>
public override int GetInt(int min, int max)
{
return Random.Next(min, max);
}
/// <summary>
/// Gets a float value between 0.0 and 1.0.
/// </summary>
/// <returns>
/// The float value.
/// </returns>
public override float GetFloat()
{
return (float)Random.NextDouble();
}
/// <summary>
/// Gets a double value between 0.0 and 1.0.
/// </summary>
/// <returns>The double value.</returns>
public override double GetDouble()
{
return Random.NextDouble();
}
#endregion
}
}