Search code examples
c++timerandomstandard-libraryseed

How can I get rid of the warning with rand()? (C++)


Whenever I use the rand function in C++:

#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;
}

I get a warning about conversion from time_t to int at line 5:

srand(time(0));

Is there any way to get rid of this warning?


Solution

  • Actually, you should be using an an unsigned int with srand():

    srand((unsigned) time(0));