概率随机数发生器

假设我正在编写一个简单的运气游戏 – 每个玩家按Enter键,游戏会在1-6之间为他分配一个随机数。 就像一个立方体。 在游戏结束时,数量最多的玩家获胜。

现在,让我们说我是个骗子。 我想写游戏,所以玩家#1(将是我)的概率为90%得到6,而2%得到每个剩下的数字(1,2,3,4,5)。

如何随机生成数字,并设置每个数字的概率?

static Random random = new Random(); static int CheatToWin() { if (random.NextDouble() < 0.9) return 6; return random.Next(1, 6); } 

另一种可定制的作弊方式:

 static int IfYouAintCheatinYouAintTryin() { List> iAlwaysWin = new List>(); iAlwaysWin.Add(new Tuple(0.02, 1)); iAlwaysWin.Add(new Tuple(0.04, 2)); iAlwaysWin.Add(new Tuple(0.06, 3)); iAlwaysWin.Add(new Tuple(0.08, 4)); iAlwaysWin.Add(new Tuple(0.10, 5)); iAlwaysWin.Add(new Tuple(1.00, 6)); double realRoll = random.NextDouble(); // same random object as before foreach (var cheater in iAlwaysWin) { if (cheater.Item1 > realRoll) return cheater.Item2; } return 6; } 

你有几个选择,但一种方法是拉1到100之间的数字,并使用你的权重将其分配给骰子面数。

所以

 1,2 = 1 3,4 = 2 5,6 = 3 7,8 = 4 9,10 = 5 11-100 = 6 

这将为您提供所需的比率,并且以后也很容易调整。

你可以定义分布数组(伪代码):

//公平分配

 array = {0.1666, 0.1666, 0.1666, 0.1666, 0.1666, 0.1666 }; 

然后将骰子从0滚动到1,保存到x然后执行

 float sum = 0; for (int i = 0; i < 6;i++) { sum += array[i]; if (sum > x) break; } 

我是骰子号码。

现在,如果你想欺骗改变数组:

 array = {0.1, 0.1, 0.1, 0.1, 0.1, 0.5 }; 

并且你将有50%得到6(而不是16%)