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

时间:2013-12-27 21:28:49

标签: 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(随机数)在数组中为红色,它也会返回。

到woolstar:这是我实际代码的简化版本。我希望生成一个随机数(r),然后根据数组红色检查这个数字,如果r在数组中我希望它被认为是真的并且在这种情况下打印你好

E.g。 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

2 个答案:

答案 0 :(得分:3)

  

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 << "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

答案 1 :(得分:2)

由于我认为这是主要问题的一个问题,我会忽略(如果这就是这个评论)行

r = rand() % 3;

如果r为1,则打印hello。否则先执行else。这打印再见并打破循环。

for if永远不会超过一个循环迭代,因为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

我猜这不是你想要的,并建议一个标志可能是最好的方法,在循环完成后留下打印。