forked from andrewkirillov/AForge.NET
-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathExponentialGenerator.cs
124 lines (113 loc) · 3.5 KB
/
ExponentialGenerator.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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
// AForge Math Library
// AForge.NET framework
// http://www.aforgenet.com/framework/
//
// Copyright © AForge.NET, 2007-2011
// contacts@aforgenet.com
//
namespace AForge.Math.Random
{
using System;
/// <summary>
/// Exponential random numbers generator.
/// </summary>
///
/// <remarks><para>The random number generator generates exponential
/// random numbers with specified rate value (lambda).</para>
///
/// <para>The generator uses <see cref="UniformOneGenerator"/> generator as a base
/// to generate random numbers.</para>
///
/// <para>Sample usage:</para>
/// <code>
/// // create instance of random generator
/// IRandomNumberGenerator generator = new ExponentialGenerator( 5 );
/// // generate random number
/// float randomNumber = generator.Next( );
/// </code>
/// </remarks>
///
public class ExponentialGenerator : IRandomNumberGenerator
{
private UniformOneGenerator rand = null;
private float rate = 0;
/// <summary>
/// Rate value (inverse mean).
/// </summary>
///
/// <remarks>The rate value should be positive and non zero.</remarks>
///
public float Rate
{
get { return rate; }
}
/// <summary>
/// Mean value of the generator.
/// </summary>
///
public float Mean
{
get { return 1.0f / rate; }
}
/// <summary>
/// Variance value of the generator.
/// </summary>
///
public float Variance
{
get { return 1f / ( rate * rate ); }
}
/// <summary>
/// Initializes a new instance of the <see cref="ExponentialGenerator"/> class.
/// </summary>
///
/// <param name="rate">Rate value.</param>
///
/// <exception cref="ArgumentException">Rate value should be greater than zero.</exception>
///
public ExponentialGenerator( float rate ) :
this( rate, 0 )
{
}
/// <summary>
/// Initializes a new instance of the <see cref="ExponentialGenerator"/> class.
/// </summary>
///
/// <param name="rate">Rate value (inverse mean).</param>
/// <param name="seed">Seed value to initialize random numbers generator.</param>
///
/// <exception cref="ArgumentException">Rate value should be greater than zero.</exception>
///
public ExponentialGenerator( float rate, int seed )
{
// check rate value
if ( rate <= 0 )
throw new ArgumentException( "Rate value should be greater than zero." );
this.rand = new UniformOneGenerator( seed );
this.rate = rate;
}
/// <summary>
/// Generate next random number
/// </summary>
///
/// <returns>Returns next random number.</returns>
///
public float Next( )
{
return - (float) Math.Log( rand.Next( ) ) / rate;
}
/// <summary>
/// Set seed of the random numbers generator.
/// </summary>
///
/// <param name="seed">Seed value.</param>
///
/// <remarks>Resets random numbers generator initializing it with
/// specified seed value.</remarks>
///
public void SetSeed( int seed )
{
rand = new UniformOneGenerator( seed );
}
}
}