gpt4 book ai didi

c# - 概率随机数发生器

转载 作者:可可西里 更新时间:2023-11-01 02:59:23 24 4
gpt4 key购买 nike

假设我正在编写一个简单的运气游戏 - 每个玩家按下 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<Tuple<double, int>> iAlwaysWin = new List<Tuple<double, int>>();
iAlwaysWin.Add(new Tuple<double, int>(0.02, 1));
iAlwaysWin.Add(new Tuple<double, int>(0.04, 2));
iAlwaysWin.Add(new Tuple<double, int>(0.06, 3));
iAlwaysWin.Add(new Tuple<double, int>(0.08, 4));
iAlwaysWin.Add(new Tuple<double, int>(0.10, 5));
iAlwaysWin.Add(new Tuple<double, int>(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;
}

关于c# - 概率随机数发生器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3016439/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com