File size: 1,774 Bytes
b1b3bae
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
/// ------------------------------------------------------
/// SwarmOps - Numeric and heuristic optimization for C#
/// Copyright (C) 2003-2011 Magnus Erik Hvass Pedersen.
/// Please see the file license.txt for license details.
/// SwarmOps on the internet: http://www.Hvass-Labs.org/
/// ------------------------------------------------------

using System.Diagnostics;

namespace SwarmOps
{
    public static partial class Tools
    {
        /// <summary>
        /// Pick a uniform random sample from [x-range, x+range].
        /// </summary>
        public static double Sample(double x, double range)
        {
            Debug.Assert(range >= 0);

            // Pick a sample from within the bounded range.
            double y = Globals.Random.Uniform(x - range, x + range);

            return y;
        }

        /// <summary>
        /// First bound the sampling range [x-range, x+range] using lowerBound
        /// and upperBound respectively, and then pick a uniform random sample
        /// from the bounded range. This avoids samples being boundary points.
        /// </summary>
        public static double SampleBounded(double x, double range, double lowerBound, double upperBound)
        {
            Debug.Assert(lowerBound < upperBound);
            Debug.Assert(range >= 0);

            double l, u;	// Sampling range.
            double y;		// Actual sample.

            // Adjust sampling range so it does not exceed bounds.
            l = System.Math.Max(x - range, lowerBound);
            u = System.Math.Min(x + range, upperBound);

            // Pick a sample from within the bounded range.
            y = Globals.Random.Uniform(l, u);

            return y;
        }
    }
}