Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/131.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/http/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 轮盘赌随机数和if语句c++;_C++ - Fatal编程技术网

C++ 轮盘赌随机数和if语句c++;

C++ 轮盘赌随机数和if语句c++;,c++,C++,我正在做一个轮盘赌桌,想问一个问题,但也有修复代码的帮助 1,目前我正在使用rand()%37作为0-36个数字的随机数生成器。是否有更好的生成器提供更多随机数 2、我不确定如何对照if语句检查数组中的值。(检查下面的代码) intred[18]={1,3,5,7,9,12,14,16,18,19,21,23,25,27,30,32,34,36}; int main() { srand(时间(空)); r=rand()%3; cout因为我认为这是对主要问题的转移,所以我将忽略这句话(如果这句话

我正在做一个轮盘赌桌,想问一个问题,但也有修复代码的帮助

1,目前我正在使用rand()%37作为0-36个数字的随机数生成器。是否有更好的生成器提供更多随机数

2、我不确定如何对照if语句检查数组中的值。(检查下面的代码)

intred[18]={1,3,5,7,9,12,14,16,18,19,21,23,25,27,30,32,34,36};
int main()
{
srand(时间(空));
r=rand()%3;

cout因为我认为这是对主要问题的转移,所以我将忽略这句话(如果这句话是这样的话)

如果r为1,将打印hello。否则,将首先执行else。这将打印bye并中断循环

for永远不能运行过一次循环迭代,因为if的两个子句中都有break语句——如果这样编写循环,则更容易看到(我强烈建议您养成更经常使用括号的习惯)


我猜这不是您想要的,建议使用一个标志可能是最好的方式,让打印在循环完成后进行。

因为我认为这是主要问题的一个小问题,我将忽略(如果这是此评论的话)这一行

如果r为1,将打印hello。否则,将首先执行else。这将打印bye并中断循环

for永远不能运行过一次循环迭代,因为if的两个子句中都有break语句——如果这样编写循环,则更容易看到(我强烈建议您养成更经常使用括号的习惯)

我猜这不是你想要的,建议使用一个标志可能是最好的方式,让打印在循环完成后进行

1,目前我正在使用rand()%37作为0-36数字的随机数生成器。是否有更好的生成器提供更多随机数

<绝对是。请考虑使用C++ 11随机数设施(或者Boosiv.NoDy,如果你没有C++ 11)。 请参阅以下参考资料:

编辑请参见以下内容: 希望这将足够引人注目,可以抛弃
rand()

1,目前我正在使用rand()%37作为0-36数字的随机数生成器。是否有更好的生成器提供更多随机数

<绝对是。请考虑使用C++ 11随机数设施(或者Boosiv.NoDy,如果你没有C++ 11)。 请参阅以下参考资料:

编辑请参见以下内容: 希望这将足够引人注目,可以抛弃
rand()


你想让你的程序做什么?你想让你的程序做什么?我使用了find和random generator,完美地处理了1种情况。当我制作一个轮盘赌表时,我将处理多个情况,你能告诉我如何在2种情况下使用它吗,即红色数组和黑色数组?如果你想让我给出正确的代码f或者只是颜色的情况下,我会做的,但我会使用这个多次!我问这个问题,因为我试图使用完全相同的第二个案件再次(完全相同的甲酸作为第一个案件)我用了find和random generator,在1个案例中效果很好。当我制作一个轮盘赌表时,我将有多个案例,你能告诉我如何在2个案例中使用它吗?即红色数组和黑色数组?如果你想让我为颜色的案例设置正确的代码,我会这样做,但我会的我问这个问题是因为我试图再次使用与第一个案例完全相同的格式(与第一个案例完全相同的格式),但返回时出现了许多错误!
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;
}
// 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
r = rand() % 3;
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
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);
#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;
}