|
| 1 | +namespace FastFibCode.Benchmark; |
| 2 | + |
| 3 | +using static Distribution; |
| 4 | + |
| 5 | +public static class DistributionData |
| 6 | +{ |
| 7 | + private static readonly Dictionary<Distribution, uint[]> values = new Dictionary<Distribution, uint[]>(); |
| 8 | + private static readonly Dictionary<Distribution, ulong[]> codes = new Dictionary<Distribution, ulong[]>(); |
| 9 | + |
| 10 | + public static uint[] GetValues(Distribution distribution) => values[distribution]; |
| 11 | + public static ulong[] GetCodes(Distribution distribution) => codes[distribution]; |
| 12 | + |
| 13 | + public static void Init() |
| 14 | + { |
| 15 | + values.Clear(); |
| 16 | + |
| 17 | + values[FoldedNormal_100] = Create(() => FoldedNormalDistributionRandom(100)); |
| 18 | + values[FoldedNormal_1K] = Create(() => FoldedNormalDistributionRandom(1000)); |
| 19 | + values[FoldedNormal_10K] = Create(() => FoldedNormalDistributionRandom(10_000)); |
| 20 | + values[FoldedNormal_100K] = Create(() => FoldedNormalDistributionRandom(100_000)); |
| 21 | + values[FoldedNormal_1M] = Create(() => FoldedNormalDistributionRandom(1000_000)); |
| 22 | + values[FoldedNormal_10M] = Create(() => FoldedNormalDistributionRandom(10_000_000)); |
| 23 | + |
| 24 | + values[Exponential_100] = Create(() => ExponentialDistributionRandom(100)); |
| 25 | + values[Exponential_1K] = Create(() => ExponentialDistributionRandom(1000)); |
| 26 | + values[Exponential_10K] = Create(() => ExponentialDistributionRandom(10_000)); |
| 27 | + values[Exponential_100K] = Create(() => ExponentialDistributionRandom(100_000)); |
| 28 | + values[Exponential_1M] = Create(() => ExponentialDistributionRandom(1_000_000)); |
| 29 | + values[Exponential_10M] = Create(() => ExponentialDistributionRandom(10_000_000)); |
| 30 | + |
| 31 | + values[Uniform_5M] = Create(() => (uint)Random.Shared.Next(0, 5_000_000)); |
| 32 | + |
| 33 | + codes.Clear(); |
| 34 | + foreach (var p in values) |
| 35 | + { |
| 36 | + var cs = new ulong[p.Value.Length]; |
| 37 | + for (int i = 0; i < p.Value.Length; i++) |
| 38 | + cs[i] = Fibonacci.EncodeUInt(p.Value[i]); |
| 39 | + codes[p.Key] = cs; |
| 40 | + } |
| 41 | + } |
| 42 | + |
| 43 | + private static uint[] Create(Func<uint> rnd) |
| 44 | + { |
| 45 | + uint[] data = new uint[1000_000]; |
| 46 | + for (int i = 0; i < data.Length; i++) |
| 47 | + data[i] = rnd(); |
| 48 | + return data; |
| 49 | + } |
| 50 | + |
| 51 | + public static uint ExponentialDistributionRandom(uint sigma) |
| 52 | + => (uint)Math.Round(-Math.Log(1 - Random.Shared.NextDouble()) * sigma); |
| 53 | + |
| 54 | + public static uint FoldedNormalDistributionRandom(uint sigma) |
| 55 | + { |
| 56 | + // https://en.wikipedia.org/wiki/Folded_normal_distribution |
| 57 | + double s = sigma; |
| 58 | + var normalSigma = Math.Sqrt(s * s / (1 - 2 / Math.PI)); |
| 59 | + double u1, u2; |
| 60 | + do |
| 61 | + { |
| 62 | + u1 = Random.Shared.NextDouble(); |
| 63 | + } |
| 64 | + while (u1 == 0); |
| 65 | + u2 = Random.Shared.NextDouble(); |
| 66 | + |
| 67 | + var mag = normalSigma * Math.Sqrt(-2.0 * Math.Log(u1)); |
| 68 | + return (uint)Math.Abs(mag * Math.Cos(2 * Math.PI * u2)); |
| 69 | + } |
| 70 | +} |
0 commit comments