概率随机数发生器

Alo*_*kin 17 c# random probability

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

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

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

Ant*_*ram 21

static Random random = new Random();

static int CheatToWin()
{
    if (random.NextDouble() < 0.9)
        return 6;

    return random.Next(1, 6);
}
Run Code Online (Sandbox Code Playgroud)

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

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;
}
Run Code Online (Sandbox Code Playgroud)

  • 哦,当然,这是一种*可定制的欺骗手段.我会加一个. (3认同)

Mat*_*nes 5

您有几种选择,但一种方法是提取 1 到 100 之间的数字,然后使用您的权重将其分配给骰子面的数字。

所以

1,2 = 1
3,4 = 2
5,6 = 3
7,8 = 4
9,10 = 5
11-100 = 6
Run Code Online (Sandbox Code Playgroud)

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