public void GetInt_DontHaveProbability() { try { _weightedRandom.GetInt(); Assert.Fail(); } catch (ArgumentException) { } catch (Exception) { Assert.Fail(); } }
void DoShow(Dictionary <int, int> probabilities) { WeightedRandom weightedRandom = new WeightedRandom(probabilities); Dictionary <int, int> numberTime = probabilities.ToDictionary(p => p.Key, p => 0); // 第二个 Lambda 以 KeyValuePair 为参数,但直接返回 0 for (int i = 0; i < _loopTime; i++) { numberTime[weightedRandom.GetInt()]++; } StringBuilder sb = new StringBuilder("获取" + _loopTime + "次随机数\n"); foreach (KeyValuePair <int, int> item in numberTime) { sb.Append(item.Key + ":" + item.Value + "次:" + item.Value / (float)_loopTime * 100 + "%\n"); } _resultText.text = sb.ToString(); }