【发布时间】:2010-10-23 12:14:40
【问题描述】:
每当我在 C++ 中使用 rand 函数时:
#include<iostream>
#include<time.h>
#include<stdlib.h>
using namespace std;
int main(){
srand(time(0));
int n=(rand()%6)+1;
cout<<"The dice roll is "<<n<<"."<<endl;
}
我在第 5 行收到关于从 time_t 转换为 int 的警告:
srand(time(0));
有什么办法可以消除这个警告吗?
【问题讨论】:
-
你应该在 C++ 代码中使用
和 。 -
为什么要删除 C++ 的 C 标签?问题是关于 srand() 和 time(),它们来自 C 语言。
标签: c++ time random standard-library seed