From bbfce82f52f214f3f2c44a5b410b0db0d861c432 Mon Sep 17 00:00:00 2001 From: MrTarantula Date: Tue, 24 Jul 2018 09:46:01 -0500 Subject: [PATCH] JK --- GamblersDice.Tests/FairDieTests.cs | 8 --- GamblersDice.Tests/GamblersDice.Tests.Ref.cs | 75 -------------------- GamblersDice/FairDie.cs | 13 +--- GamblersDice/GamblersDice.csproj | 2 +- GamblersDice/GamblersDie.cs | 25 ++----- README.md | 12 ---- 6 files changed, 9 insertions(+), 126 deletions(-) delete mode 100644 GamblersDice.Tests/GamblersDice.Tests.Ref.cs diff --git a/GamblersDice.Tests/FairDieTests.cs b/GamblersDice.Tests/FairDieTests.cs index 2f93160..997c289 100644 --- a/GamblersDice.Tests/FairDieTests.cs +++ b/GamblersDice.Tests/FairDieTests.cs @@ -23,14 +23,6 @@ public class FairDieTests [Trait("Category", "Random")] public void ConstructFairDie_Sides_Random() => Assert.IsType(new FairDie(_rnd, 6)); - [Fact] - [Trait("Category", "Reference")] - public void ConstructFairDie_Ref() => Assert.IsType(new FairDie(ref _rnd)); - - [Fact] - [Trait("Category", "Reference")] - public void ConstructFairDie_Sides_Ref() => Assert.IsType(new FairDie(ref _rnd, 6)); - [Theory] [InlineData(6)] [InlineData(20)] diff --git a/GamblersDice.Tests/GamblersDice.Tests.Ref.cs b/GamblersDice.Tests/GamblersDice.Tests.Ref.cs deleted file mode 100644 index 879f1a2..0000000 --- a/GamblersDice.Tests/GamblersDice.Tests.Ref.cs +++ /dev/null @@ -1,75 +0,0 @@ -using System; -using Xunit; -using GamblersDice; - -namespace GamblersDice.Tests -{ - [Trait("Category", "Gamblers")] - [Trait("Category", "Reference")] - public class GamblersDieTestsRef - { - Random _rnd = new Random(); - - [Fact] - public void ConstructDie() => Assert.IsType(new GamblersDie(ref _rnd)); - - [Fact] - public void ConstructDie_Sides() => Assert.IsType(new GamblersDie(ref _rnd, 6)); - - [Fact] - public void ConstructDie_Weights() => Assert.IsType(new GamblersDie(ref _rnd, 1, 2, 3, 4, 5, 6)); - - [Fact] - public void ConstructDie_Weights_Array() => Assert.IsType(new GamblersDie(ref _rnd, new int[] { 1, 2, 3, 4, 5, 6 })); - - [Theory] - [InlineData(6)] - [InlineData(20)] - public void Uniform(int sides) - { - var die = new GamblersDie(ref _rnd, sides); - int[] result = new int[sides]; - decimal iters = 10_000_000M; - - for (int i = 0; i < iters; i++) - { - result[die.Roll() - 1]++; - } - - for (int i = 0; i < sides; i++) - { - decimal roll = (result[i] - iters / sides) / iters; - Assert.True(0.001M > roll, $"{roll} is outside of uniformity tolerance of 0.001"); - } - } - - [Theory] - [InlineData(new int[] { 0, 0, 1, 0, 0, 0 }, 3)] - [InlineData(new int[] { 0, 0, 0, 0, 1, 0 }, 5)] - public void NonUniform(int[] weights, int expected) => Assert.Equal(expected, new GamblersDie(ref _rnd, weights).Roll()); - - [Fact] - public void NonUniform_Inferred() - { - // Given this state, [4,3,2,1] - // 1 should be rolled 40% of the time - // 2 should be rolled 30% of the time - // 3 should be rolled 20% of the time - // 4 should be rolled 10% of the time - - decimal iters = 100_000_000M; - int[] result = new int[] { 0, 0, 0, 0 }; - - for (int run = 0; run < iters; run++) - { - var die = new GamblersDie(ref _rnd, 4, 3, 2, 1); - result[die.Roll() - 1]++; - } - - Assert.True(Math.Abs(0.4M - result[0] / iters) < 0.001M, $"Side one is outside of uniformity tolerance: {Math.Abs(0.4M - result[0] / iters)}"); - Assert.True(Math.Abs(0.3M - result[1] / iters) < 0.001M, $"Side two is outside of uniformity tolerance: {Math.Abs(0.3M - result[1] / iters)}"); - Assert.True(Math.Abs(0.2M - result[2] / iters) < 0.001M, $"Side three is outside of uniformity tolerance: {Math.Abs(0.2M - result[2] / iters)}"); - Assert.True(Math.Abs(0.1M - result[3] / iters) < 0.001M, $"Side four is outside of uniformity tolerance: {Math.Abs(0.1M - result[3] / iters)}"); - } - } -} \ No newline at end of file diff --git a/GamblersDice/FairDie.cs b/GamblersDice/FairDie.cs index a53dca3..dd711ec 100644 --- a/GamblersDice/FairDie.cs +++ b/GamblersDice/FairDie.cs @@ -16,21 +16,12 @@ public FairDie(int sides) : this(new Random(), sides) { } /// Initializes a new fair die with a default of six sides. Bring your own Random object. /// Random object to be used when rolling the die - public FairDie(Random rnd) : this(ref rnd) { } + public FairDie(Random rnd) : this(rnd, 6) { } /// Initializes a new fair die with the specified number of sides. Bring your own Random object. /// Random object to be used when rolling the die. /// Number of sides on the die. - public FairDie(Random rnd, int sides) : this(ref rnd, sides) { } - - /// Initializes a new fair die with a default of six sides using reference to a Random object. - /// Reference to Random object to be used when rolling the die. - public FairDie(ref Random rnd) : this(ref rnd, 6) { } - - /// Initializes a new fair die with the specified number of sides using reference to a Random object. - /// Reference to Random object to be used when rolling the die. - /// Number of sides on the die. - public FairDie(ref Random rnd, int sides) + public FairDie(Random rnd, int sides) { _rnd = rnd; _sides = sides; diff --git a/GamblersDice/GamblersDice.csproj b/GamblersDice/GamblersDice.csproj index 8c93319..6c58ea5 100644 --- a/GamblersDice/GamblersDice.csproj +++ b/GamblersDice/GamblersDice.csproj @@ -8,7 +8,7 @@ GamblersDice Gambler's Dice This set of dice follows the Gambler's fallacy. A C# port of https://github.com/xori/gamblers-dice - 1.2.0 + 1.3.0 Ryan Tauriainen https://github.com/MrTarantula/gamblers-dice/blob/master/LICENSE https://github.com/MrTarantula/gamblers-dice diff --git a/GamblersDice/GamblersDie.cs b/GamblersDice/GamblersDie.cs index caccdc9..81b7e88 100644 --- a/GamblersDice/GamblersDie.cs +++ b/GamblersDice/GamblersDie.cs @@ -22,26 +22,13 @@ public GamblersDie(params int[] weights) : this(new Random(), weights) { } /// Initializes a new gambler's die with a default of six sides. Bring your own Random object. /// Random object to be used when rolling the die. - public GamblersDie(Random rnd) : this(ref rnd) { } + public GamblersDie(Random rnd) : this(rnd, 6) { } /// Initializes a new gambler's die with the specified number of sides. Bring your own Random object. /// Random object to be used when rolling the die. /// Size of the die. - public GamblersDie(Random rnd, int size) : this(ref rnd, size) { } - - /// Initializes a new gambler's die with known weights. Bring your own Random object. - /// Random object to be used when rolling the die. - /// Pre-calculated weights of the sides of the die. - public GamblersDie(Random rnd, params int[] weights) : this(ref rnd, weights) { } - - /// Initializes a new gambler's die with a default of six sides using reference to a Random object. - /// Reference to Random object to be used when rolling the die. - public GamblersDie(ref Random rnd) : this(ref rnd, 6) { } - - /// Initializes a new gambler's die with the specified number of sides using reference to a Random object. - /// Reference to Random object to be used when rolling the die. - /// Size of the die. - public GamblersDie(ref Random rnd, int size) { + public GamblersDie(Random rnd, int size) + { _rnd = rnd; Weight = new int[size]; @@ -51,10 +38,10 @@ public GamblersDie(ref Random rnd, int size) { } } - /// Initializes a new gambler's die with known weights using reference to a Random object. - /// Reference to Random object to be used when rolling the die. + /// Initializes a new gambler's die with known weights. Bring your own Random object. + /// Random object to be used when rolling the die. /// Pre-calculated weights of the sides of the die. - public GamblersDie(ref Random rnd, params int[] weights) : this(ref rnd, weights.Length) + public GamblersDie(Random rnd, params int[] weights) : this(rnd, weights.Length) { for (int i = 0; i < Weight.Length; i++) { diff --git a/README.md b/README.md index 7cd9b5d..9f860ad 100644 --- a/README.md +++ b/README.md @@ -103,16 +103,4 @@ var die0 = new GamblersDie(rnd); var die1 = new GamblersDie(rnd, 1, 1, 1, 7, 1, 1, 1); var die2 = new FairDie(rnd, 20); -``` - -This can also be done with a reference to a `System.Random` object, but I'm currently not sure if there are any performance gains: - -```C# -var rnd = new Random(); - -var die0 = new GamblersDie(ref rnd); - -var die1 = new GamblersDie(ref rnd, 1, 1, 1, 7, 1, 1, 1); - -var die2 = new FairDie(ref rnd, 20); ``` \ No newline at end of file