using System; using System.Collections.Generic; using System.Text; namespace ET { public static class MathHelper { public static int RoundToInt(float d) { return (int)MathF.Round(d, MidpointRounding.AwayFromZero); } public static long RoundToLong(float d) { return (long)MathF.Round(d, MidpointRounding.AwayFromZero); } public static bool IsHit(float p) { return p > RandomHelper.RandomFloat(); } public static int GetProbabilityIndex(float[] probabilities) { int count = probabilities.Length; float a = RandomHelper.RandomFloat(); int index = -1; float add = 0; foreach (float item in probabilities) { ++index; if(index+1 == count) { return index; } if (add < a && a <= add+probabilities[index]) { return index; } add += item; } return 0; } public static int GetProbabilityIndexByWeight(List weights) { int totalWeight = 0; foreach (int item in weights) { totalWeight += item; } int count = weights.Count; int a = RandomHelper.RandomNumber(0,totalWeight+1); int index = -1; float add = 0; foreach (int item in weights) { ++index; if (index + 1 == count) { return index; } if (add < a && a <= add + weights[index]) { return index; } add += item; } return 0; } } }