gpt4 book ai didi

c++ - 轮盘随机数和if语句c++

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

我正在制作轮盘赌 table ,想问一个问题,但也有人帮助我修复代码。

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(随机数)在数组 red 中,它也会返回 bye。

致 woolstar:这是我实际代码的简化版本。我想生成一个随机数 (r),然后根据数组 red 检查这个数字,如果 r 在数组中,我希望它被视为 true 并在这种情况下打印 hello

例如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, currently I am using rand()%37 as my random number generator for the 0-36 numbers. Is there a better generator that gives a more random number?

绝对是的。请考虑使用 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, I am unsure how to check values in an array against an if statement. (check code below)

我会使用 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 << "hello\n";
}
else {
std::cout << "hello\n";
}
return 0;
}

请参阅以下引用资料:

http://en.cppreference.com/w/cpp/algorithm/find

编辑也请看这个:

希望这足以吸引人放弃 rand()

http://channel9.msdn.com/Events/GoingNative/2013/rand-Considered-Harmful

关于c++ - 轮盘随机数和if语句c++,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20809406/

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