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

C++自定义函数实现随机数程序 相同元音输入返回重复值问题

问题描述
  • 实现了一款简易随机数程序,支持接收5个输入字符并输出对应生成的随机数
  • 程序接收5个不区分大小写的元音字符(a/e/i/o/u,大小写均可),通过自定义函数基于输入计算返回随机数
  • 测试输入示例:a A a A e
  • 预期输出示例:1 2 3 19 25
  • 异常表现:程序正常运行时,多次输入相同元音字符始终返回相同随机数值;在调试器中加断点运行时,该异常不会出现

原实现代码:

#include <iostream>
#include <ctime>
#include <cstdlib>
#include <iomanip>

using namespace std;

int createRandomFromChar(char inputChar);

int main()
{
    char answer;
   
    char inputOne, inputTwo, inputThree, inputFour, inputFive;

    cout << endl <<
        "This program plays a simple random number guessing game." << endl;

    do
    {
        cout << endl << "Enter 5 vowel characters (a,e,i,o,u or A,E,I,O,U) separated by spaces: ";
        cin >> inputOne >> inputTwo >> inputThree >> inputFour >> inputFive;
        cin.ignore();
       
        int randomNumberOne = createRandomFromChar(inputOne);
        int randomNumberTwo = createRandomFromChar(inputTwo);
        int randomNumberThree = createRandomFromChar(inputThree);
        int randomNumberFour = createRandomFromChar(inputFour);
        int randomNumberFive = createRandomFromChar(inputFive);
        

        cout << "The random numbers are " << left << 
            setw(3) << randomNumberOne << left <<
            setw(3) << randomNumberTwo << left <<
            setw(3) << randomNumberThree << left << setw(3) << randomNumberFour 
            << left << setw(3) << randomNumberFive;

       

        cout << endl << "Do you want to continue playing? Enter 'Y' or 'y' to continue playing: "
            << endl;
        
       
        answer = cin.get();

        cin.ignore();
    }
    while ((answer == 'y') || (answer == 'Y'));

}

int createRandomFromChar(char inputChar)
{
    srand(time(0));

    int n1 = 1 + (rand() % 20);
    int n2 = 21 + (rand() % 20);
    int n3 = 41 + (rand() % 20);
    int n4 = 61 + (rand() % 20);
    int n5 = 81 + (rand() % 20);

    if ((inputChar == 'a') || (inputChar == 'A'))
    {
        return n1;

    }
    else if ((inputChar == 'e') || (inputChar == 'E'))
    {
        return n2;

    }
    else if ((inputChar == 'i') || (inputChar == 'I'))
    {
        return n3;

    }
    else if ((inputChar == 'o') || (inputChar == 'O'))
    {
        return n4;

    }
    else if ((inputChar == 'u') || (inputChar == 'U'))
    {
        return n5;

    }
    else
    {
        return 0;
    }
    
}
故障原因
  • 错误将随机数种子初始化语句srand(time(0))写在了自定义生成函数createRandomFromChar内部,每次调用函数都会重置随机数生成器的种子
  • time(0)返回值是秒级精度的Unix时间戳,程序正常运行时,连续5次调用createRandomFromChar的总耗时远小于1秒,传入srand的种子值完全一致,重置后rand()生成的随机数序列完全相同,相同输入对应的返回值自然不会变化
  • 调试时设置断点会拉长函数调用的间隔,相邻两次调用的时间差超过1秒时,time(0)返回值发生变化,种子不同生成的随机数就不同,因此异常无法复现
修复方案
  • 随机数种子在整个程序生命周期内只需要初始化一次,将srand(time(0))语句从createRandomFromChar函数中删除
  • 在main函数最开头、进入业务循环前添加srand(time(0));完成一次性初始化即可

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.30 11:57:22