加权随机数生成的C#随机数

由网友(年轻不懂忄生)分享简介:如何能随机生成两种状态之一,与概率红色所产生的10%的时间,而绿色所产生的90%的时间?How can I randomly generate one of two states, with the probability of 'red' being generated 10% of the time, and '...

如何能随机生成两种状态之一,与概率红色所产生的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();
}
阅读全文

相关推荐

最新文章