为什么在从 time(NULL) 播种随机数生成器时会收到有关可能丢失数据的警告?

发布于 2024-12-07 12:29:49 字数 991 浏览 0 评论 0原文

我正在学习向量并编写了一些代码来选择我可以用来在荷兰购买彩票的随机数。但尽管它运行了,编译器还是警告我“从‘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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

亽野灬性zι浪 2024-12-14 12:29:49

由于在您的特定平台上 time_t 的大小恰好大于 unsigned int,因此您会收到这样的警告。从“较大”类型转换为“较小”类型涉及截断和数据丢失,但在您的特定情况下,这并不重要,因为您只是为随机数生成器播种并溢出 unsigned int 应该出现在很远的将来的某个日期。

将其显式转换为 unsigned int 应该会抑制警告:

srand((unsigned int) time(NULL));

Because time_t happens to be larger in size than unsigned 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 an unsigned int should occur for a date in the very far future.

Casting it to unsigned int explicitly should suppress the warning:

srand((unsigned int) time(NULL));
暖伴 2024-12-14 12:29:49

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 while unsigned 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.

喜你已久 2024-12-14 12:29:49

time 返回一个 time_t 对象。

srand 需要一个无符号整数。

time returns a time_t object.

srand is expecting an unsigned int.

2024-12-14 12:29:49
srand(time(NULL));

如果 time 的返回值超出 unsigned int 的表示范围,则该行可能会溢出,这当然是可能的。

srand(time(NULL));

This line can overflow if the return value from time exceeds the representation range of an unsigned int, which is certainly possible.

无敌元气妹 2024-12-14 12:29:49
void srand ( unsigned int seed );
time_t time ( time_t * timer );
typedef long int __time_t;

long int 与 unsigned int 不同。因此发出警告。

(来自 stackoverflow

void srand ( unsigned int seed );
time_t time ( time_t * timer );
typedef long int __time_t;

long int is not the same as a unsigned int. Hence the warning.

(from stackoverflow

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文