"Double or Nothing"赌博机器代码无法超过15组合
"Double or Nothing" gambling machine code can't reach past 15 combo
所以我写了一个代码来模拟游戏中的赌博机。基本上,你按下一个按钮,你就有机会将你获得的积分翻倍。另一方面,如果你失败了,你必须重新开始。示例运行可能是:
Start of run 1:
0
1
2
Start of run 2:
0
1
Start of run 3:
0
Start of run 4:
0
1
2
它工作正常。我让代码执行一定数量的运行(由用户输入到"n"中确定(并输出在所有这些运行中达到的最大组合。它还告诉何时超越了最高组合。
问题是,在一定数量的运行之后,无论出于何种原因,最高组合都不能超过 15。从字面上看,每次我输入 1000 万或更多(,它都会给出 15。鉴于它根本不匹配概率,这似乎不对。
我播种的方式有问题吗?
#include <iostream>
#include <stdlib.h>
//#include "stdafx.h"
#include<ctime>
using namespace std;
int main() {
int n = 1;
srand(time(0));
while (n > 0) {
cin >> n;
int highestCombo = 0;
for (int i = 0; i < n; i++) {
int combo = 0;
while (true) {
int r = (rand() % 2) + 1;
if (r == 1) {
combo++;
}
else if (r == 2) {
if (combo > highestCombo) {
highestCombo = combo;
cout << combo << " at #" << i << endl;
}
combo = 0;
break;
}
}
}
cout << "Highest Combo: " << highestCombo << endl;
}
}
编辑:所以似乎它可能只是我的IDE。奇怪。我正在使用 Dev-C++,因为我只是想快速编写它。然而,cpp.sh 超过15岁,进入20多岁。
正确答案接缝来自tobi303。我为您测试了他的解决方案,并使用"><随机>"效果更好。随机>
#include <iostream>
#include <stdlib.h>
//#include "stdafx.h"
#include<ctime>
using namespace std;
int main() {
int n = 1;
//srand(time(NULL));
std::mt19937 rng;
rng.seed(std::random_device()());
std::uniform_int_distribution<std::mt19937::result_type> rand(1,2);
while (n > 0) {
cin >> n;
int highestCombo = 0;
for (int i = 0; i < n; i++) {
int combo = 0;
while (true) {
//int r = (rand() % 2);
int r = rand(rng);
if (r == 1) {
combo++;
}
else if (r == 2) {
if (combo > highestCombo) {
highestCombo = combo;
cout << combo << " at #" << i << endl;
}
combo = 0;
break;
}
}
if(i == n - 1)
{
cout << " i " << i << endl;
}
}
cout << "Highest Combo: " << highestCombo << endl;
}
}
相关文章:
- C++:TypeDef使用元组
- Mongodb c++驱动程序:如何查询元素的数组
- ArduinoJson 6.15.2:JsonObject没有命名类型
- 将数组的地址分配给变量并删除
- 从C++本机插件更新Vector3数组
- 如何在C++中从两个单独的for循环中添加两个数组
- 计算数组c++的平均值
- lambda参数转换为constexpr技巧,然后获取带链接的数组
- 将数组作为参数传递给函数安全吗?作为第三方职能部门,可以探索他们想要的之外的其他元素
- 数组索引的值没有增加
- 在C++中手动调整数组大小
- 将对象数组的引用传递给函数
- 为char数组调整zlib-zpipe
- "Double or Nothing"赌博机器代码无法超过15组合
- 将 (*abc) + 15 未定义,因为数组只有 10 个字符长
- 如何在 C++ 中将 char 数组中的前 15 个字符转换为 std::string
- MISRA C++-2008规则5-0-15-数组索引应是指针算术的唯一形式
- 创建一个15维数组时会发生什么
- 为什么在我的机器上std::vector的分配和释放比动态数组慢
- 如何在机器学习课程中使用数组