]> git.smokeofanarchy.ru Git - space-station-14.git/commitdiff
Binomial Number Selector for EntityTables (#36783)
authorIProduceWidgets <107586145+IProduceWidgets@users.noreply.github.com>
Mon, 21 Apr 2025 00:57:20 +0000 (20:57 -0400)
committerGitHub <noreply@github.com>
Mon, 21 Apr 2025 00:57:20 +0000 (02:57 +0200)
* binomial dist

* lol initialize the int tho idiot

* xml docs my forgotten

* ditto

Content.Shared/EntityTable/ValueSelector/BinomialNumberSelector.cs [new file with mode: 0644]

diff --git a/Content.Shared/EntityTable/ValueSelector/BinomialNumberSelector.cs b/Content.Shared/EntityTable/ValueSelector/BinomialNumberSelector.cs
new file mode 100644 (file)
index 0000000..b3ed076
--- /dev/null
@@ -0,0 +1,38 @@
+using Robust.Shared.Random;
+
+namespace Content.Shared.EntityTable.ValueSelector;
+
+/// <summary>
+/// Picks a value based on a Binomial Distribution of N Trials given P Chance
+/// </summary>
+public sealed partial class BinomialNumberSelector : NumberSelector
+{
+    /// <summary>
+    /// How many times to try including an entry. i.e. the Max.
+    /// </summary>
+    [DataField]
+    public int Trials = 1;
+
+    /// <summary>
+    /// The odds a single trial succeeds
+    /// </summary>
+    /// <remarks>
+    /// my preferred "Prob" was already used in other places for entity table stuff and I didnt want more confusing terminology
+    /// </remarks>
+    [DataField]
+    public float Chance = .5f;
+
+    public override int Get(System.Random rand)
+    {
+        var random = IoCManager.Resolve<IRobustRandom>();
+        int count = 0;
+
+        for (int i = 0; i < Trials; i++)
+        {
+            if (random.Prob(Chance))
+                count++;
+        }
+        return count;
+        // get binomialed motherfucker
+    }
+}