// Decompiled with JetBrains decompiler // Type: Terraria.Utilities.WeightedRandom`1 // Assembly: Terraria, Version=1.4.0.5, Culture=neutral, PublicKeyToken=null // MVID: 67F9E73E-0A81-4937-A22C-5515CD405A83 // Assembly location: C:\Users\mikeyisbaeyt\Downloads\depotdownloader-2.4.5\depots\105601\6707058\Terraria.exe using System; using System.Collections.Generic; using System.Linq; namespace Terraria.Utilities { public class WeightedRandom { public readonly List> elements = new List>(); public readonly UnifiedRandom random; public bool needsRefresh = true; private double _totalWeight; public WeightedRandom() => this.random = new UnifiedRandom(); public WeightedRandom(int seed) => this.random = new UnifiedRandom(seed); public WeightedRandom(UnifiedRandom random) => this.random = random; public WeightedRandom(params Tuple[] theElements) { this.random = new UnifiedRandom(); this.elements = ((IEnumerable>) theElements).ToList>(); } public WeightedRandom(int seed, params Tuple[] theElements) { this.random = new UnifiedRandom(seed); this.elements = ((IEnumerable>) theElements).ToList>(); } public WeightedRandom(UnifiedRandom random, params Tuple[] theElements) { this.random = random; this.elements = ((IEnumerable>) theElements).ToList>(); } public void Add(T element, double weight = 1.0) { this.elements.Add(new Tuple(element, weight)); this.needsRefresh = true; } public T Get() { if (this.needsRefresh) this.CalculateTotalWeight(); double num = this.random.NextDouble() * this._totalWeight; foreach (Tuple element in this.elements) { if (num <= element.Item2) return element.Item1; num -= element.Item2; } return default (T); } public void CalculateTotalWeight() { this._totalWeight = 0.0; foreach (Tuple element in this.elements) this._totalWeight += element.Item2; this.needsRefresh = false; } public void Clear() => this.elements.Clear(); public static implicit operator T(WeightedRandom weightedRandom) => weightedRandom.Get(); } }