while 循环中不会发生随机化。如何解决这个问题? C++

Randomization doesn't occur in while loop. How to fix this? C++

当 if/else 出现在此 while 循环中时,我无法进行随机化。这是代码:

#include<iostream>
#include<cstdlib>
#include<ctime>
using namespace std;

int main(){
    srand(time(0));
    int dice_num=(rand()% 6+1)+(rand()%6 +1);
    char dice_value,your_guess;


    while(true){

    //Dice Conversion
        if(dice_num%2==0) dice_value='c';
         else dice_value='h';

//User Input
                cout<<"Enter 'c'(even) or 'h'(odd)or 'e' to exit : ";
                cin>>your_guess;

        //User Math
                if(your_guess==dice_value){

                    cout<<"You Won!"<<endl;
                }else {

                    cout<<"You Lost!"<<endl;
                }

                cout<<your_guess<<dice_value<<endl;
                if(your_guess=='e') break;

    }
}

我希望 dice_value 始终是随机的,无论是 'c' 还是 'h' 但是当循环重复时,值不会改变。我哪里错了?

您需要移动这条语句:

int dice_num=(rand()% 6+1)+(rand()%6 +1);

进入你的 while 循环。