forked from giacomelli/GeneticSharp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRandomizationBase.cs
110 lines (97 loc) · 3.78 KB
/
RandomizationBase.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
using System;
using System.Linq;
using GeneticSharp.Infrastructure.Framework.Texts;
namespace GeneticSharp.Domain.Randomizations
{
/// <summary>
/// Base class for randomization.
/// </summary>
public abstract class RandomizationBase : IRandomization
{
#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 abstract int GetInt(int min, int max);
/// <summary>
/// Gets a float value between 0.0 and 1.0.
/// </summary>
/// <returns>
/// The float value.
/// </returns>
public abstract float GetFloat();
/// <summary>
/// Gets a double value between 0.0 and 1.0.
/// </summary>
/// <returns>The double value.</returns>
public abstract double GetDouble();
/// <summary>
/// Gets an integer array with values between minimum value (inclusive) and maximum value (exclusive).
/// </summary>
/// <returns>The integer array.</returns>
/// <param name="length">The array length</param>
/// <param name="min">Minimum value (inclusive).</param>
/// <param name="max">Maximum value (exclusive).</param>
public virtual int[] GetInts(int length, int min, int max)
{
var ints = new int[length];
for (int i = 0; i < length; i++)
{
ints[i] = GetInt(min, max);
}
return ints;
}
/// <summary>
/// Gets an integer array with unique values between minimum value (inclusive) and maximum value (exclusive).
/// </summary>
/// <returns>The integer array.</returns>
/// <param name="length">The array length</param>
/// <param name="min">Minimum value (inclusive).</param>
/// <param name="max">Maximum value (exclusive).</param>
public virtual int[] GetUniqueInts(int length, int min, int max)
{
var diff = max - min;
if (diff < length)
{
throw new ArgumentOutOfRangeException(
nameof(length),
"The length is {0}, but the possible unique values between {1} (inclusive) and {2} (exclusive) are {3}.".With(length, min, max, diff));
}
var orderedValues = Enumerable.Range(min, diff).ToList();
var ints = new int[length];
for (int i = 0; i < length; i++)
{
var removeIndex = GetInt(0, orderedValues.Count);
ints[i] = orderedValues[removeIndex];
orderedValues.RemoveAt(removeIndex);
}
return ints;
}
/// <summary>
/// Gets a float value between minimum value (inclusive) and maximum value (exclusive).
/// </summary>
/// <param name="min">Minimum value.</param>
/// <param name="max">Max value.</param>
/// <returns>
/// The float value.
/// </returns>
public float GetFloat(float min, float max)
{
return min + ((max - min) * GetFloat());
}
/// <summary>
/// Gets a double value between minimum value (inclusive) and maximum value (exclusive).
/// </summary>
/// <returns>The double value.</returns>
/// <param name="min">Minimum value.</param>
/// <param name="max">Max value.</param>
public virtual double GetDouble(double min, double max)
{
return min + ((max - min) * GetDouble());
}
#endregion
}
}