forked from giacomelli/GeneticSharp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRandomizationExtensions.cs
42 lines (39 loc) · 1.58 KB
/
RandomizationExtensions.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
namespace GeneticSharp
{
/// <summary>
/// Extension methods for IRandomization.
/// </summary>
public static class RandomizationExtensions
{
/// <summary>
/// Gets an even integer value between minimum value (inclusive) and maximum value (exclusive).
/// </summary>
/// <returns>The integer.</returns>
/// <param name="randomization">The IRandomization implementation.</param>
/// <param name="min">Minimum value (inclusive).</param>
/// <param name="max">Maximum value (exclusive).</param>
public static int GetEvenInt(this IRandomization randomization, int min, int max)
{
checked
{
var candidate = randomization.GetInt(min, max - 1);
return candidate % 2 == 0 ? candidate : candidate + 1;
}
}
/// <summary>
/// Gets an odd integer value between minimum value (inclusive) and maximum value (exclusive).
/// </summary>
/// <returns>The integer.</returns>
/// <param name="randomization">The IRandomization implementation.</param>
/// <param name="min">Minimum value (inclusive).</param>
/// <param name="max">Maximum value (exclusive).</param>
public static int GetOddInt(this IRandomization randomization, int min, int max)
{
checked
{
var candidate = randomization.GetInt(min, max - 1);
return candidate % 2 != 0 ? candidate : candidate + 1;
}
}
}
}