gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 13:00:37 25 4
gpt4 key购买 nike

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

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 = 1Process 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.632305Calculated: 0.63125

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

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