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

Roulette random number and if statement c++

本文关键字:语句 c++ if 随机数      更新时间:2023-10-16

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

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

我猜这不是您想要的,并建议标志可能是最好的方法,将打印留到循环完成后。