You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

C++骰子程序问题:重新掷骰时无法生成新随机数

Fixing the Re-Roll Issue in Your Dice Game

Hey there! I see exactly what's going on with your dice game—when you enter "R" to re-roll, the dice values stay stuck because you're only generating those random numbers once, before your loop even starts. Let's break this down and get it fixed.

The Root Cause

Right now, your roll1, roll2, and UserRoll variables are initialized outside the outer do-while loop. That means they get their random values one time when the program launches, and never update again—even when you loop back to re-roll. The loop just keeps printing the original values instead of creating new ones.

Also, quick note: You're calling srand() correctly at the start of the program (only once, which is the best practice—calling it every roll would actually lead to worse randomness since the timestamp might not change fast enough between quick rolls).

The Fix

Move the random number generation and total calculation inside the outer do-while loop. This way, every time the loop runs (including when you choose to re-roll), it will generate brand new random values for the dice.

Modified Code

Here's the updated version of your code with the fix applied:

#include <stdlib.h> /// need this for srand() -- for random numbers
#include <time.h> /// need this for time() -- time
#include <iostream> /// need this for cout<< and cin>>
using namespace std; /// need this for cout<< and cin>>

int main() {
    int iseed = (int)time(0);
    srand(iseed); // Only call srand ONCE at the start of the program
    cout << "Beat the computer! \n";
    
    char keep;
    int UserRoll; // Declare this outside the loop to use it for the final comparison
    do {
        // Generate new dice rolls EVERY time the loop runs
        int roll1 = 1 + rand() % 6;
        int roll2 = 1 + rand() % 6;
        UserRoll = roll1 + roll2;
        
        cout << "You rolled a " << roll1 << " and a " << roll2 << " for a total of: " << UserRoll << "\n";
        cout << "\n";
        
        do {
            cout << "Would you like to keep this total, or roll again? \n";
            cout << "\n";
            cout << "Enter \"K\" for keep and \"R\" for roll again: \n";
            cin >> keep;
            
            if (keep != 'K' && keep != 'R') {
                cout << "That is not a valid choice. Please choose K to keep your total or R to roll again. " << endl;
                cout << "\n";
            }
        } while(keep != 'K' && keep != 'R');
        
        if (keep == 'R') {
            cout << "You chose R--let's roll again. \n";
        } else {
            cout << "Great! Your total is " << UserRoll << "\n";
        }
    } while (keep == 'R');
    
    // Computer's roll (unchanged logic here)
    int roll3 = 1 + rand() % 6;
    int roll4 = 1 + rand() % 6;
    int ComputerRoll = roll3 + roll4;
    
    cout << "The computer rolled a " << roll3 << " and a " << roll4 << " for a total of: " << ComputerRoll << "\n";
    cout << "\n";
    
    // Cleaned up condition checks for readability
    if (ComputerRoll < UserRoll) {
        cout << "Congratulations! You won! \n";
    } else if (ComputerRoll > UserRoll) {
        cout << "Sorry. You lose. \n";
    } else {
        cout << "It's a tie. \n";
    }
    
    return 0;
}

Key Changes Explained

  • Moved roll1, roll2, and UserRoll calculation inside the outer loop: Now every iteration (including re-rolls) generates fresh random numbers.
  • Declared UserRoll outside the loop: So we can access its final value when comparing against the computer's roll later.
  • Switched to else if for the final result checks: Makes the code flow a bit cleaner, but this is just a minor readability tweak.

Now when you enter "R", the program will generate new dice values each time—exactly what you wanted!

内容的提问来源于stack exchange,提问作者amykp

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.29 07:07:51