加权随机数生成的C# [英] Weighted Random Number Generation in C#
本文介绍了加权随机数生成的C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何能随机生成两种状态之一,与概率红色所产生的10%的时间,而绿色所产生的90%的时间?
How can I randomly generate one of two states, with the probability of 'red' being generated 10% of the time, and 'green' being generated 90% of the time?
每2秒一个绿色或红色指示灯将闪烁。
Every 2 second either a green or a red light will blink.
此序列将持续5分钟。
This sequence will continue for 5 minutes.
出现一个闪烁的光的总数应为300。
The total number of occurrences of a blinking light should be 300.
推荐答案
Random.NextDouble返回一个数字0和1之间,所以下面应该工作:
Random.NextDouble returns a number between 0 and 1, so the following should work:
if (random.NextDouble() < 0.90)
{
BlinkGreen();
}
else
{
BlinkRed();
}
这篇关于加权随机数生成的C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文