gpt4 book ai didi

c++ - 我怎样才能摆脱 rand() 的警告? (C++)

转载 作者:塔克拉玛干 更新时间:2023-11-03 08:05:27 24 4
gpt4 key购买 nike

每当我在 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));

有什么办法可以消除这个警告吗?

最佳答案

实际上,您应该使用 unsigned int with srand() :

srand((unsigned) time(0));

关于c++ - 我怎样才能摆脱 rand() 的警告? (C++),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/817975/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com