【发布时间】:2015-03-01 15:07:36
【问题描述】:
假设我有 12 个值。对于这种情况,让每个值是一年中的一个月。现在把每个月写在一张单独的纸上,然后放在桌子上。
所以我们有 12 张纸沿着桌子的表面排成一排,每张纸上都写着一年中的月份。
每个月都有一定的百分比可供选择。例如,一月 = 15%,二月 = 9%,十二月 = 5%,等等。
现在,让我们随机选择一个介于 1 和 100 之间的数字。我们可以使用两个 10 面骰子,使用计算机随机数生成器,或者任何你想要的。
好的,让我们看看写着“一月”的那张纸。我们现在生成一个介于 1 到 100 之间的随机数,结果为 43。好吧,我们现在进入 2 月份,因为我们正在寻找 15 或更低的数字。看看二月,我们生成一个介于 1 和 100 之间的新随机数并掷出 87。同样,我们正在寻找 9 或以下来选择二月,所以我们移至三月。依此类推……直到我们到了 7 月,生成的随机数是 3!好的,选择了 7 月,我们停止了整个过程。七月是赢家。
我的问题是:每个月真的有被选中的百分比价值机会吗?因为如果一个被选中,我们会停止处理剩余的几个月,我认为不会。如果我们每个月都处理而不考虑被选中,那么是的,每个月都有被选中的面值百分比。
如果我们必须在每个月被选中后绝对停止(我们确实必须这样做),我怎样才能使每个月都有被选中的真实百分比机会?
我认为这个过程的一个步骤是确保每个月都有一个值,如果所有这些加起来等于 100,我已经做到了。但这真的能确保什么吗?
代码方面 (c++),它的外观如下:
// 15% January
// 9% February
// 9% March
// 8% April
// 8% May
// 8% June
// 8% July
// 8% August
// 8% September
// 8% October
// 6% November
// 5% December
int month = 0;
bool monthFound = false;
if (!monthFound && random(1, 100) <= 15)
{
monthFound = true;
month = 1;
}
if (!monthFound && random(1, 100) <= 9)
{
monthFound = true;
month = 2;
}
if (!monthFound && random(1, 100) <= 9)
{
monthFound = true;
month = 3;
}
if (!monthFound && random(1, 100) <= 8)
{
monthFound = true;
month = 4;
}
if (!monthFound && random(1, 100) <= 8)
{
monthFound = true;
month = 5;
}
if (!monthFound && random(1, 100) <= 8)
{
monthFound = true;
month = 6;
}
if (!monthFound && random(1, 100) <= 8)
{
monthFound = true;
month = 7;
}
if (!monthFound && random(1, 100) <= 8)
{
monthFound = true;
month = 8;
}
if (!monthFound && random(1, 100) <= 8)
{
monthFound = true;
month = 9;
}
if (!monthFound && random(1, 100) <= 8)
{
monthFound = true;
month = 10;
}
if (!monthFound && random(1, 100) <= 6)
{
monthFound = true;
month = 11;
}
if (!monthFound && random(1, 100) <= 5)
{
monthFound = true;
month = 12;
}
if (month)
printf("The month that was picked is: %d", month);
else
printf("No month was picked.");
这也是另一种方式:
int monthNum = 0;
std::vector<int> chance_;
std::vector<int> month_;
month_.push_back(1); chance_.push_back(15);
month_.push_back(2); chance_.push_back(9);
month_.push_back(3); chance_.push_back(9);
month_.push_back(4); chance_.push_back(8);
month_.push_back(5); chance_.push_back(8);
month_.push_back(6); chance_.push_back(8);
month_.push_back(7); chance_.push_back(8);
month_.push_back(8); chance_.push_back(8);
month_.push_back(9); chance_.push_back(8);
month_.push_back(10); chance_.push_back(8);
month_.push_back(11); chance_.push_back(6);
month_.push_back(12); chance_.push_back(5);
for (std::vector<std::string>::size_type z = 0; z < month_.size(); z++)
{
// Check the chance of the month being picked.
if (RANDOM(1, 100) <= chance_[z])
{
monthNum = month_[z];
break;
}
}
if (monthNum)
printf("The month that was picked is: %d", monthNum);
else
printf("No month was picked.");
【问题讨论】:
-
我看不出这与编程有什么关系。你为什么要为每个月选择一个新的随机数?在 [0-100) 之间抽取一个随机数并说如果是 [0-15) 是 1 月,如果是 [15-24) 是 2 月等等,难道不够吗?
-
不一定和编程有关。这是一个百分比/统计问题。也许我发错地方了。版主可以把这个帖子移到适当的地方吗?我很抱歉。
-
克里斯是对的。这意味着它是准确的。
-
啊,是的,我现在看到了。我想多了。这确实会产生我正在寻找的结果。谢谢! (只是不确定为什么对这个问题投反对票)
标签: percentage percentile