概率随机数生成器

发布于 2024-09-05 07:32:58 字数 197 浏览 3 评论 0原文

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

现在,假设我是一个骗子。我想编写游戏,以便玩家#1(即我)有 90% 的概率获得 6,并有 2% 的概率获得其余数字(1、2、3、4、5)。

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

Let's say I'm writing a simple luck game - each player presses Enter and the game assigns him a random number between 1-6. Just like a cube. At the end of the game, the player with the highest number wins.

Now, let's say I'm a cheater. I want to write the game so player #1 (which will be me) has a probability of 90% to get six, and 2% to get each of the rest numbers (1, 2, 3, 4, 5).

How can I generate a number random, and set the probability for each number?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

帅气称霸 2024-09-12 07:32:59
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;
}
static Random random = new Random();

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

    return random.Next(1, 6);
}

Another customizable way to cheat:

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;
}
青春如此纠结 2024-09-12 07:32:59

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

因此,

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

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

You have a few options, but one way would be to pull a number between 1 and 100, and use your weights to assign that to a dice face number.

So

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

this would give you the ratios you need, and would also be fairly easy to tune later.

难理解 2024-09-12 07:32:59

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

//公平分布

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

然后将骰子从0掷到1,保存到x然后do

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

i是骰子数。

现在如果你想作弊,将数组更改为:

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

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

you can define array of distribution (pseudocode) :

//fair distribution

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

then roll the dice from 0 to 1, save to x then do

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

i is the dice number.

now if you want to cheat change array to:

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

and you will have 50% to get 6 (instead of 16%)

ζ澈沫 2024-09-12 07:32:59

你觉得这个卡斯提尔怎么样?

输入数据:

int range = { 0, 2, 4, 6, 8, 92}
int rand = Random.range(1, 100) 

方法本身:

public int Selectionbyrange()   
{
    List<Tuple<int, int, int>> ran_r_i  = new List<Tuple<int, int, int>>();
    // Creating 3 lists
    for (int i = 1; i < (int)range.LongLength;i++)                     
    {
        ran_r_i.Add(new Tuple<int, int, int>(range[i - 1], range[i], i));
        // Writing to the list of ranges
    }

    foreach (var ran_i in ran_r_i)
    {
    if (ran_i.Item1 < rand && rand <= ran_i.Item2)              
        return ran_i.Item3;
        // Range check
    }  

    return 1;    
}

作为随机结果,我们得到
这样的数据:

当rand > 时0 且 <= 2 我们有 1

  • 当 rand > 时2 且 <= 4 我们有 2

  • 当 rand > 时4 和 <= 6 我们有 3

  • 当 rand > 时6 且 <= 8 我们有 4

  • 当 rand > 时8 且 <= 92 我们有 5

  • 当 rand > 时92 且 <= 100 我们有 6

And how do you like this castile?

Input data:

int range = { 0, 2, 4, 6, 8, 92}
int rand = Random.range(1, 100) 

The method itself:

public int Selectionbyrange()   
{
    List<Tuple<int, int, int>> ran_r_i  = new List<Tuple<int, int, int>>();
    // Creating 3 lists
    for (int i = 1; i < (int)range.LongLength;i++)                     
    {
        ran_r_i.Add(new Tuple<int, int, int>(range[i - 1], range[i], i));
        // Writing to the list of ranges
    }

    foreach (var ran_i in ran_r_i)
    {
    if (ran_i.Item1 < rand && rand <= ran_i.Item2)              
        return ran_i.Item3;
        // Range check
    }  

    return 1;    
}

As a result of random we get
such data:

When rand > 0 and <= 2 we have 1

  • When rand > 2 and <= 4 we have 2

  • When rand > 4 and <= 6 we have 3

  • When rand > 6 and <= 8 we have 4

  • When rand > 8 and <= 92 we have 5

  • When rand > 92 and <= 100 we have 6

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文