代码运行,但随机数生成器只输出数字2.我输入了空洞代码,以防万一我想不到任何因素。是否有更方便的方法来处理随机数生成器?
#include <windows.h>
#include <stdio.h>
#include <tchar.h>
#include <iostream>
#include <string>
#include <chrono>
#include <thread>
int Time();
int RanNum1to10();
void main()
{
time_t now = time(0);
tm *ltm = localtime(&now);
int hour = ltm->tm_hour;
int min = 1 + ltm->tm_min;
int sec = 1 + ltm->tm_sec;
int RanNum = RanNum1to10();
std::string text;
std::getline(std::cin, text);
{
if(text.find("hi") != std::string::npos)
{
RanNum1to10();
if (RanNum >= 1 && RanNum <= 4)
{
Time();
if(hour >= 0 && hour <= 11)
{
std::cout << "good morning" << std::endl;
}
if(hour >= 12 && hour <= 17)
{
std::cout << "good afternoon" << std::endl;
}
if(hour >= 18 && hour <= 23)
{
std::cout << "good evening" << std::endl;
}
}
if (RanNum >= 5 && RanNum <= 6)
{
{
std::cout << "hey" << std::endl;
}
}
if (RanNum >= 7 && RanNum <= 8)
{
{
std::cout << "hello" << std::endl;
}
}
if (RanNum >= 9 && RanNum <= 10)
{
{
std::cout << "hi" << std::endl;
}
}
std::cout << RanNum;
std::cout << std::endl;
}
}
system("pause");
}
int RanNum1to10()
{
return (rand() % 10 + 1);
}
int Time()
{
time_t now = time(0);
tm *ltm = localtime(&now);
int hour = ltm->tm_hour;
int min = 1 + ltm->tm_min;
int sec = 1 + ltm->tm_sec;
return hour && min && sec;
}
答案 0 :(得分:1)
在使用rand()之前,您需要致电
srand(time(NULL));
启动随机数生成器。