为什么在从 time(NULL) 播种随机数生成器时会收到有关可能丢失数据的警告?
我正在学习向量并编写了一些代码来选择我可以用来在荷兰购买彩票的随机数。但尽管它运行了,编译器还是警告我“从‘time_t’转换为‘无符号整数,可能会丢失数据’”。
谁能找出是什么原因造成的吗?我什至没有在这段代码中定义任何 unsigned int ;据我了解,int i 默认是一个有符号的 int 。感谢您的见解。
#include <iostream>
#include <vector>
#include <string>
#include <ctime>
using namespace std;
void print_numbers();
string print_color();
int main() {
srand(time(NULL));
print_numbers();
string color = print_color();
cout << color << endl;
system("PAUSE");
return 0;
}
//Fill vector with 6 random integers.
//
void print_numbers() {
vector<int> lucky_num;
for (int i = 0; i < 6; i++) {
lucky_num.push_back(1 + rand() % 45);
cout << lucky_num.at(i) << endl;
}
}
//Select random color from array.
//
string print_color() {
string colors[6] = {"red", "orange", "yellow", "blue", "green", "purple"};
int i = rand()%6;
return colors[i];
}
确切的编译器消息:警告 C4244:'argument':从 'time_t' 到 'unsigned int' 的转换,可能会丢失数据。 11 号线。
am learning vectors and made a bit of code that selects random numbers i can use for buying lottery tickets here in Netherlands. But although it runs, the compiler is warning me about 'conversion from 'time_t' to 'unsigned int, possible loss of data'.
Can anyone spot what is causing this? I haven't even defined any unsigned int in this code; int i by default is a signed int as i understand. Thanks for insight.
#include <iostream>
#include <vector>
#include <string>
#include <ctime>
using namespace std;
void print_numbers();
string print_color();
int main() {
srand(time(NULL));
print_numbers();
string color = print_color();
cout << color << endl;
system("PAUSE");
return 0;
}
//Fill vector with 6 random integers.
//
void print_numbers() {
vector<int> lucky_num;
for (int i = 0; i < 6; i++) {
lucky_num.push_back(1 + rand() % 45);
cout << lucky_num.at(i) << endl;
}
}
//Select random color from array.
//
string print_color() {
string colors[6] = {"red", "orange", "yellow", "blue", "green", "purple"};
int i = rand()%6;
return colors[i];
}
Exact compiler message: warning C4244: 'argument': conversion from 'time_t' to 'unsigned int', possible loss of data. Line 11.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
由于在您的特定平台上
time_t
的大小恰好大于unsigned int
,因此您会收到这样的警告。从“较大”类型转换为“较小”类型涉及截断和数据丢失,但在您的特定情况下,这并不重要,因为您只是为随机数生成器播种并溢出unsigned int 应该出现在很远的将来的某个日期。
将其显式转换为
unsigned int
应该会抑制警告:Because
time_t
happens to be larger in size thanunsigned int
on your particular platform, you get such a warning. Casting from a "larger" to a "smaller" type involves truncating and loss of data, but in your particular case it doesn't matter so much because you are just seeding the random number generator and overflowing anunsigned int
should occur for a date in the very far future.Casting it to
unsigned int
explicitly should suppress the warning:time_t
在许多平台上是 64 位值,以防止纪元时间最终回绕,而unsigned int
是 32 位。就您而言,您不在乎,因为您只是播种随机数生成器。但在其他代码中,如果您的软件曾经处理过过去的日期2038,当您转换为 32 位时,您的
time_t
可能会被截断为 2038 年之前的 32 位日期 价值。time_t
is a 64 bit value on many platforms to prevent the epoch time eventually wrapping whileunsigned int
is 32 bits.In your case, you don't care cause you're just seeding the random number generator. But in other code, if your software ever deals in dates past 2038, you could have your
time_t
truncated to a 32-bit pre 2038 date when you cast to a 32-bit value.time 返回一个
time_t
对象。srand 需要一个无符号整数。
time returns a
time_t
object.srand is expecting an unsigned int.
如果
time
的返回值超出unsigned int
的表示范围,则该行可能会溢出,这当然是可能的。This line can overflow if the return value from
time
exceeds the representation range of anunsigned int
, which is certainly possible.long int 与 unsigned int 不同。因此发出警告。
(来自 stackoverflow
long int is not the same as a unsigned int. Hence the warning.
(from stackoverflow