private static void ValidateByNameEnumDistributesWithCorrectWeights <TEnum>(int itemsPerBucket, float tolerance, int sampleSizePercentage) where TEnum : struct { itemsPerBucket = (itemsPerBucket * sampleSizePercentage) / 100; tolerance = (tolerance * 100) / sampleSizePercentage; var random = XorShift128Plus.Create(seed); var weights = new Dictionary <string, int>(); int weightSum = 0; var buckets = new Dictionary <TEnum, int>(); var enumNames = System.Enum.GetNames(typeof(TEnum)); foreach (var name in enumNames) { int weight = random.RangeCC(3, 10); weights.Add(name, weight); weightSum += weight; } foreach (var name in enumNames) { TEnum enumValue = (TEnum)System.Enum.Parse(typeof(TEnum), name); int currentTarget; if (buckets.TryGetValue(enumValue, out currentTarget)) { buckets[enumValue] = currentTarget + itemsPerBucket * enumNames.Length * weights[name] / weightSum; } else { buckets.Add(enumValue, itemsPerBucket * enumNames.Length * weights[name] / weightSum); } } int iterations = 0; foreach (var target in buckets.Values) { iterations += target; } var generator = random.MakeWeightedEnumGenerator <TEnum>((System.Func <string, int>)((string name) => weights[name])); for (int i = 0; i < iterations; ++i) { int currentTarget; TEnum enumValue = generator.Next(); if (buckets.TryGetValue(enumValue, out currentTarget)) { buckets[enumValue] = currentTarget - 1; } else { Assert.Fail("An invalid enum value was generated."); } } int[] bucketsArray = new int[buckets.Count]; buckets.Values.CopyTo(bucketsArray, 0); Assert.LessOrEqual(RandomeEngineTests.CalculateStandardDeviation(bucketsArray, 0), tolerance * itemsPerBucket); }
public static void ValidateHalfClosedDoubleUnitBucketDistribution(IRandom random, int bucketCount, int hitsPerBucket, float tolerance, int sampleSizePercentage) { hitsPerBucket = (hitsPerBucket * sampleSizePercentage) / 100; tolerance = (tolerance * 100) / sampleSizePercentage; var buckets = new int[bucketCount]; for (int i = 0; i < bucketCount * hitsPerBucket; ++i) { var n = random.DoubleOC(); buckets[(int)System.Math.Ceiling(n * bucketCount) - 1] += 1; } Assert.LessOrEqual(RandomeEngineTests.CalculateStandardDeviation(buckets, hitsPerBucket), tolerance * hitsPerBucket); }
public static void ValidateHalfOpenFloatUnitBucketDistribution(IRandom random, int bucketCount, int hitsPerBucket, float tolerance, int sampleSizePercentage) { hitsPerBucket = (hitsPerBucket * sampleSizePercentage) / 100; tolerance = (tolerance * 100) / sampleSizePercentage; var buckets = new int[bucketCount]; for (int i = 0; i < bucketCount * hitsPerBucket; ++i) { var n = random.FloatCO(); buckets[Mathf.FloorToInt(n * bucketCount)] += 1; } Assert.LessOrEqual(RandomeEngineTests.CalculateStandardDeviation(buckets, hitsPerBucket), tolerance * hitsPerBucket); }
private static void ValidateByNameEnumDistributesUniformly <TEnum>(int itemsPerBucket, float tolerance, int sampleSizePercentage) where TEnum : struct { itemsPerBucket = (itemsPerBucket * sampleSizePercentage) / 100; tolerance = (tolerance * 100) / sampleSizePercentage; var buckets = new Dictionary <TEnum, int>(); var enumValues = System.Enum.GetValues(typeof(TEnum)); foreach (var value in enumValues) { TEnum enumValue = (TEnum)value; int currentTarget; if (buckets.TryGetValue(enumValue, out currentTarget)) { buckets[enumValue] = currentTarget + itemsPerBucket; } else { buckets.Add(enumValue, itemsPerBucket); } } int iterations = itemsPerBucket * enumValues.Length; var random = XorShift128Plus.Create(seed); var generator = random.MakeEnumGenerator <TEnum>(true); for (int i = 0; i < iterations; ++i) { int currentTarget; TEnum enumValue = generator.Next(); if (buckets.TryGetValue(enumValue, out currentTarget)) { buckets[enumValue] = currentTarget - 1; } else { Assert.Fail("An invalid enum value was generated."); } } int[] bucketsArray = new int[buckets.Count]; buckets.Values.CopyTo(bucketsArray, 0); Assert.LessOrEqual(RandomeEngineTests.CalculateStandardDeviation(bucketsArray, 0), tolerance * itemsPerBucket); }
private static void ValidateShuffleDistributesUniformly(int bucketCount, int iterations, float tolerance, System.Func <IRandom, IList <int> > shuffle, int sampleSizePercentage) { iterations = (iterations * sampleSizePercentage) / 100; tolerance = (tolerance * 100) / sampleSizePercentage; var hitsPerBucket = (bucketCount / 2) * iterations; int[] sumArray = new int[bucketCount]; var random = XorShift128Plus.Create(seed); for (int i = 0; i < iterations; ++i) { IList <int> list = shuffle(random); for (int j = 0; j < list.Count; ++j) { sumArray[j] += list[j]; } } Assert.LessOrEqual(RandomeEngineTests.CalculateStandardDeviation(sumArray, hitsPerBucket), tolerance * hitsPerBucket); }
public void UnitVector2sUniformlyDistributed(int sampleSizePercentage) { var bucketCount = 72; var hitsPerBucket = 144; var tolerance = 0.1f; hitsPerBucket = (hitsPerBucket * sampleSizePercentage) / 100; tolerance = (tolerance * 100) / sampleSizePercentage; var random = XorShift128Plus.Create(seed); var buckets = new int[bucketCount]; var twoPi = Mathf.PI * 2f; for (int i = 0; i < bucketCount * hitsPerBucket; ++i) { var v = random.UnitVector2(); var a = Mathf.Atan2(v.y, v.x); buckets[Mathf.FloorToInt((a + Mathf.PI) * bucketCount / twoPi)] += 1; } Assert.LessOrEqual(RandomeEngineTests.CalculateStandardDeviation(buckets, hitsPerBucket), tolerance * hitsPerBucket); }
private void ValidateBitDistribution64(int count, Func <ulong> generator, int bitCount, float tolerance, int sampleSizePercentage) { count = (count * sampleSizePercentage) / 100; tolerance = (tolerance * 100) / sampleSizePercentage; ulong mask = 0xFFFFFFFFFFFFFFFFUL >> (64 - bitCount); var buckets = new int[bitCount]; for (int i = 0; i < count; ++i) { ulong b = generator(); if (b > mask) { Assert.Fail(string.Format("Bit Count = {0}, Bits = 0x{1:X16}", bitCount, b)); } ulong m = 1U; for (int j = 0; j < bitCount; ++j) { if ((b & m) != 0U) { buckets[j] += 1; } m = m << 1; } } var sb = new System.Text.StringBuilder(); sb.AppendFormat("Bit Count = {0}, Buckets = [ {1}", bitCount, buckets[0]); for (int i = 1; i < bitCount; ++i) { sb.AppendFormat(", {0}", buckets[i]); } sb.Append(" ]"); Assert.LessOrEqual(RandomeEngineTests.CalculateStandardDeviation(buckets, count / 2), tolerance * count / 2, sb.ToString()); }