private UnitCollection CalculateRazings(UnitCollection sourceUnit, decimal lostPower, ref uint junkAmount) { if (lostPower <= 0.0m) { return(UnitCollection.Empty); } var amounts = sourceUnit.GetAll().Where(u => u.Amount > 0).ToArray(); // split razings between unit types and randomly deduct var powerPerAmount = this.Distribute(lostPower, amounts.Length); var razedUnits = new UnitCollection(); for (int i = 0; i < amounts.Length; i++) { uint razed = Math.Min(amounts[i].Amount, (uint)Math.Round(powerPerAmount[i] / amounts[i].Unit.Defense, 0)); razedUnits.Add(new UnitAmount(amounts[i].Unit, razed)); } return(razedUnits); }