c++ - 使用代码得出概率的近似值

标签 c++ random probability discrete-mathematics

有人给我一道关于概率的数学题。它是这样的:

There are 1000 lotteries and each has 1000 tickets. You decide to buy 1 ticket per lottery. What is the probability that you win at least one lottery?

我能够在纸上进行数学计算(达到 1 - (999/1000)^1000),但我想到了在我的计算机上进行随机实验的大量迭代的想法。所以,我输入了一些代码——确切地说是两个版本,但都出现了故障。

代码 1:

#include<iostream>
#include <stdlib.h>

using namespace std;

int main() {
    int p2 = 0;
    int p1 = 0;
    srand(time(NULL));
    for (int i = 0; i<100000; i++){
        for(int j = 0; j<1000; j++){
            int s = 0;
            int x = rand()%1000;
            int y = rand()%1000;
            if(x == y)
                s = 1;
            p1 += s;
        }
        if(p1>0)
            p2++;
    }
    cout<<"The final probability is = "<< (p2/100000);
    return 0;
}

代码 2:

#include<iostream>

#include <stdlib.h>

using namespace std;

int main() {
    int p2 = 0;
    int p1 = 0;
    for (int i = 0; i<100000; i++){
        for(int j = 0; j<1000; j++){
            int s = 0;
            srand(time(NULL));
            int x = rand()%1000;
            srand(time(NULL));
            int y = rand()%1000;
            if(x == y)
                s = 1;
            p1 += s;
        }
        if(p1>0)
            p2++;
    }
    cout<<"The final probability is = "<< (p2/100000);
    return 0;
}

代码3(引用了一些进阶的文字,但大部分都看不懂):

#include<iostream>

#include <random>

using namespace std;

int main() {
    int p2 = 0;
    int p1 = 0;
    random_device rd;
    mt19937 gen(rd());
    for (int i = 0; i<100000; i++){
        for(int j = 0; j<1000; j++){
            uniform_int_distribution<> dis(1, 1000);
            int s = 0;
            int x = dis(gen);
            int y = dis(gen);
            if(x == y)
                s = 1;
            p1 += s;
        }
        if(p1>0)
            p2++;
    }
    cout<<"The final probability is = "<< (p2/100000);
    return 0;
}

现在,所有这些代码都输出相同的文本:

The final probability is = 1
Process finished with exit code 0

It seems that the rand() function has been outputting the same value over all the 100000 iterations of the loop. I haven't been able to fix this.

I also tried using randomize() function instead of the srand() function, but it doesn't seem to work and gives weird errors like:

error: ‘randomize’ was not declared in this scope
randomize();
           ^

我认为 randomize() 已在更高版本的 C++ 中停止使用。

我知道我在很多层面上都错了。如果您能耐心地解释我的错误并让我知道一些可能的更正,我将不胜感激。

最佳答案

您应该在外循环开始时重置您的计数 (p1)。另外,请注意最后的整数除法p2/100000p2 < 100000 的任何值都会导致 0。

查看您的代码的修改版本:

#include <iostream>
#include <random>

int main()
{
    const int number_of_tests = 100000;
    const int lotteries = 1000;
    const int tickets_per_lottery = 1000;

    std::random_device rd;
    std::mt19937 gen(rd());
    std::uniform_int_distribution<> lottery(1, tickets_per_lottery);

    int winning_cases = 0;
    for (int i = 0; i < number_of_tests; ++i )
    {
        int wins = 0;                           // <- reset when each test start
        for(int j = 0; j < lotteries; ++j )
        {
            int my_ticket = lottery(gen);
            int winner = lottery(gen);
            if( my_ticket == winner )
                ++wins;
        }
        if ( wins > 0 )
            ++winning_cases;
    }
    // use the correct type to perform these calculations
    double expected = 1.0 - std::pow((lotteries - 1.0)/lotteries, lotteries);
    double probability = static_cast<double>(winning_cases) / number_of_tests;

    std::cout << "Expected: " << expected 
              << "\nCalculated: " << probability << '\n';

    return 0;
}

典型的运行会输出如下内容:

Expected: 0.632305
Calculated: 0.63125

关于c++ - 使用代码得出概率的近似值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44329647/

相关文章:

javascript - 获取一个集中在中心的随机数

c++ - 在 embarcadero/RAD studio 中跨 .dfm 文件使用常量

php - 我不明白 PHP "Daily quote"教程

c++随机数生成器,允许用户选择范围

algorithm - 对概率进行编程,让 AI 决定何时在 5 张牌扑克中弃牌

c - 如何根据概率定义rand()?

c - 在一大群数字中找到一小群数字的最佳方法

c++ - Monotouch : Adding . 资源,gcc 错误

C++ std::map 在 "*"上失败?

c++ - 链接错误 : QtCore. framework/Versions/4/QtCore for architecture x86_64