-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
BatesDistribution.cs
82 lines (75 loc) · 2.66 KB
/
BatesDistribution.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
// Copyright (c) 2020-2023 Vladimir Popov zor1994@gmail.com https://github.com/ZorPastaman/Random-Generators
using System;
using System.Runtime.CompilerServices;
using JetBrains.Annotations;
namespace Zor.RandomGenerators.ContinuousDistributions
{
/// <summary>
/// Collection of methods that generate a random value using Bates distribution algorithms.
/// </summary>
public static class BatesDistribution
{
/// <summary>
/// How many independent and identically distributed random values are generated by default.
/// </summary>
public const byte DefaultIids = 3;
/// <summary>
/// Generates a random value using <see cref="UnityGeneratorStruct.DefaultInclusive"/> as an iid source.
/// </summary>
/// <param name="iids">
/// How many independent and identically distributed random values are generated.
/// </param>
/// <returns>Generated value in range [0, 1].</returns>
/// <remarks>
/// <paramref name="iids"/> must be greater than 0.
/// </remarks>
[MethodImpl(MethodImplOptions.AggressiveInlining), Pure]
public static float Generate(byte iids)
{
return Generate(UnityGeneratorStruct.DefaultInclusive, iids);
}
/// <summary>
/// Generates a random value using <paramref name="iidFunc"/> as an iid source.
/// </summary>
/// <param name="iidFunc">
/// Function that returns an independent and identically distributed random value in range [0, 1].
/// </param>
/// <param name="iids">
/// How many independent and identically distributed random values are generated.
/// </param>
/// <returns>Generated value in range [0, 1].</returns>
/// <remarks>
/// <paramref name="iids"/> must be greater than 0.
/// </remarks>
[MethodImpl(MethodImplOptions.AggressiveInlining), Pure]
public static float Generate([NotNull] Func<float> iidFunc, byte iids)
{
return Generate(new FuncGeneratorStruct(iidFunc), iids);
}
/// <summary>
/// Generates a random value using <paramref name="iidGenerator"/> as an iid source.
/// </summary>
/// <param name="iidGenerator">
/// Random generator that returns an independent and identically distributed random value in range [0, 1].
/// </param>
/// <param name="iids">
/// How many independent and identically distributed random values are generated.
/// </param>
/// <returns>
/// Generated value in range [0, 1].
/// </returns>
/// <remarks>
/// <paramref name="iids"/> must be greater than 0.
/// </remarks>
[Pure]
public static float Generate<T>([NotNull] T iidGenerator, byte iids) where T : IContinuousGenerator
{
float random = 0f;
for (byte i = 0; i < iids; ++i)
{
random += iidGenerator.Generate();
}
return random / iids;
}
}
}