mirror of
https://github.com/DeltaV-Station/Delta-v.git
synced 2026-09-29 23:57:29 +00:00
* push * fix * push * abstract and more accurate * new abstract method I guess * that table does not explode * webedit major * Update Content.Shared/EntityTable/ValueSelector/BinomialNumberSelector.cs Co-authored-by: SnappingOpossum <snappingopossum@outlook.com> * big ass merge conflicts * sadas * review --------- Co-authored-by: Princess Cheeseballs <66055347+Pronana@users.noreply.github.com> Co-authored-by: Tayrtahn <tayrtahn@gmail.com> Co-authored-by: SnappingOpossum <snappingopossum@outlook.com>
55 lines
1.3 KiB
C#
55 lines
1.3 KiB
C#
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
|
|
}
|
|
|
|
public override float Odds()
|
|
{
|
|
if (Chance >= 1f)
|
|
return 1;
|
|
|
|
return 1 - MathF.Pow(1 - Chance, Trials);
|
|
}
|
|
|
|
public override float Average()
|
|
{
|
|
if (Chance >= 1f)
|
|
return Trials;
|
|
|
|
return Trials * Chance;
|
|
}
|
|
}
|