轮盘随机数和 if 语句 c++
Roulette random number and if statement c++
我正在制作轮盘赌桌,想问一个问题,但也有帮助修复代码。
1,目前我使用 rand()%37 作为 0-36 个数字的随机数生成器。有没有更好的生成器来给出更随机的数字?
2,我不确定如何根据 if 语句检查数组中的值。(检查下面的代码)
int red[18] = {1,3,5,7,9,12,14,16,18,19,21,23,25,27,30,32,34,36};
int main ()
{
srand(time(NULL));
r = rand() % 3;
cout << r << endl;
for(int i = 0; i<18; i++)
if (r==red[i])
{
cout << "hello" << endl;
break;
}
else
{
cout << "bye" << endl;
break;
}
return 0;
}
当我运行此代码时,即使 r(随机数)在数组中为红色,它也返回 bye。
致woolstar:这是我实际代码的简化版本。我想生成一个随机数 (r),然后对照数组红色检查这个数字,如果 r 在数组中,我希望它被认为是真的,在这种情况下打印你好
例如 r = 14,数组中的 14 是红色的吗?是:打印问候
编辑:这是我的实际代码,当运行随机数并在案例 B 和 C 中查找时,它返回错误说:初始化跳过案例标签
// Random number generator
random_device rd;
mt19937 gen (rd());
uniform_int_distribution<>
dis (0, 36);
switch (play_method)
{
case 1: // Color Play
cout << "A - Play Red.n";
cout << "B - Play Black.n";
cout << "C - Play Green.n";
cout << "Which color will you bet on?" << endl;
cin >> color;
switch (color)
{
case 'A': // Red Play
int rred = dis(gen);
auto
it = find(begin(red),end(red), rred);
if(it!=end(red))
{
win_amt = bet_amt*2;
total_amt = total_amt + (win_amt - bet_amt);
cout << "Congratulations you rolled a " << r << " and have won: $" << win_amt << "!" << endl;
cout << "You now have: $" << total_amt << " remaining." << endl;
}
else
{
total_amt = total_amt - bet_amt;
cout << "Unlucky, you rolled a " << r << " and have have lost $" << bet_amt << endl;
cout << "You have $ " << total_amt << " remaining." << endl;
} // Red play if/else end
break; // Break for case A: Red play
case 'B': // Black play
int rblack = dis(gen);
auto
it1 = find(begin(black),end(black), rblack);
if(it1!=end(black))
{
win_amt = bet_amt*2;
total_amt = total_amt + (win_amt - bet_amt);
cout << "Congratulations you rolled a " << r << " and have won: $" << win_amt << "!" << endl;
cout << "You now have: $" << total_amt << " remaining." << endl;
}
else
{
total_amt = total_amt - bet_amt;
cout << "Unlucky, you rolled a " << r << " and have have lost $" << bet_amt << endl;
cout << "You have $ " << total_amt << " remaining." << endl;
} // Black play if/else end
break; // Break for case B: Black play
case 'C':
int rgreen = dis(gen);
auto
it2 = find(begin(green),end(green), rgreen);
if(it1!=end(green))
{
win_amt = bet_amt*37;
total_amt = total_amt + (win_amt - bet_amt);
cout << "Congratulations you rolled a " << r << " and have won: $" << win_amt << "!" << endl;
cout << "You now have: $" << total_amt << " remaining." << endl;
}
else
{
total_amt = total_amt - bet_amt;
cout << "Unlucky, you rolled a " << r << " and have have lost $" << bet_amt << endl;
cout << "You have $ " << total_amt << " remaining." << endl;
} // Green play if/else end */
break;
} // Switch color end
break; // break for case 1: Color play
是1,目前我使用 rand()%37 作为 0-36 个数字的随机数生成器。有没有更好的生成器来给出更随机的数字?
的,绝对是。请考虑使用 C++11 随机数功能(如果您没有 C++11,则使用 Boost.Random)。
std::random_device rd;
std::mt19937 gen(rd());
std::uniform_int_distribution<> dis { 0, 36 };
//print random number between 0 and 36
std::cout << dis(gen);
请参阅以下参考资料:
http://en.cppreference.com/w/cpp/numeric/random
2,我不确定如何根据 if 语句检查数组中的值。(检查下面的代码)
我会使用std::find
#include <random>
#include <iostream>
#include <algorithm>
int red[18] = {1,3,5,7,9,12,14,16,18,19,21,23,25,27,30,32,34,36};
int main () {
std::random_device rd;
std::mt19937 gen { rd() };
std::uniform_int_distribution<> dis { 0, 36 };
int r=dis(gen);
auto it=std::find(std::begin(red), std::end(red), r);
if(it!=std::end(red)) {
std::cout << "hellon";
}
else {
std::cout << "hellon";
}
return 0;
}
请参阅以下参考资料:
http://en.cppreference.com/w/cpp/algorithm/find
编辑 另请参阅此内容:
希望这将足够引人注目,以抛弃rand()
。
http://channel9.msdn.com/Events/GoingNative/2013/rand-Considered-Harmful
由于我相信这是主要问题的红鲱鱼,我将忽略(如果这就是此评论的内容)该行
r = rand() % 3;
如果 r 为 1,则将打印 hello。 否则,将首先执行 else 操作。这将打印再见并打破循环。
for 永远不会超过一个循环迭代,因为 if 的两个子句中的 break 语句 - 如果你以这种方式编写循环,它更容易看到(我强烈建议你养成更频繁地使用括号的习惯)
for(int i = 0; i<18; i++)
{
if (r==red[i])
{
cout << "hello" << endl;
}
else
{
cout << "bye" << endl;
}
break;
}
return 0;
本质上是这样做的
for(int i = 0; i<18; i++)
{
// do something not affecting flow control
break;
}
return 0;
这是
int i =0;
// do something not affecting flow control
我猜这不是您想要的,并建议标志可能是最好的方法,将打印留到循环完成后。
- 我的简单if-else语句是如何无法访问的代码
- 有一个打印语句的函数是一种糟糕的编程实践吗
- 线程,如果else语句,都是错误的上下文切换后,会发生什么
- 为什么是0;C++中的有效语句
- Insert函数不适用于2 if语句C++
- If语句未被求值C++
- C++嵌套if语句,基本货币交换
- 多个If语句与使用逻辑运算符计算条件的单个语句的比较
- 是否可以使用if constexpr删除控制流语句
- 无法理解此 return 语句的功能,没有它就会发生运行时错误
- 我似乎对if/else的基本语句有问题:/
- 当调用switch语句中的函数时(即使函数不包含循环),似乎是永不结束的循环的问题
- If语句在c++中被忽略
- 比较if语句中的数组值和int值
- 以在Qt中的IF语句中设置时间延迟
- Craps游戏问题,忽略if语句
- "类模板示例<int>;"语句对 C++11 是什么意思?
- 如何编写一个使用n倍三元条件语句的C++布尔函数
- c++:定义if语句中的模板
- 主线的等价语句是什么