-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathPseudoProbability.PRD.cs
55 lines (46 loc) · 1.38 KB
/
PseudoProbability.PRD.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
namespace System
{
public partial class PseudoProbability
{
/// <summary>
/// Pseudo Random Distribution
/// </summary>
public static class PRD
{
public static float GetCFromP(float p, IMath math)
{
var upperC = p;
var lowerC = 0f;
var p2 = 1f;
float midC;
float p1;
while (true)
{
midC = (upperC + lowerC) / 2f;
p1 = GetPFromC(midC, math);
if (math.Abs(p1 - p2) <= 0f)
break;
if (p1 > p)
upperC = midC;
else
lowerC = midC;
p2 = p1;
}
return midC;
}
public static float GetPFromC(float c, IMath math)
{
var pProcByN = 0f;
var sumNpProcOnN = 0f;
var maxFails = math.CeilToInt(1f / c);
for (var N = 1; N <= maxFails; ++N)
{
var pProcOnN = math.Min(1f, N * c) * (1f - pProcByN);
pProcByN += pProcOnN;
sumNpProcOnN += N * pProcOnN;
}
return 1f / sumNpProcOnN;
}
}
}
}