5

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.

Daniel Gratz
  • 799
  • 1
  • 10
  • 15
  • It is really exciting to guess when we don't know exact compiler message and code line. However, information about lottery tickets in Netherlands really helps :) – Alex F Oct 03 '11 at 13:27
  • Warning message gives you a line, you should have mentioned this important info in the post (preferably full warning msg). – Roman R. Oct 03 '11 at 13:27
  • The warning would be on line 'srand(time(NULL));' which thats the only one talking about time. For the future a line number would indeed help. – RvdK Oct 03 '11 at 13:32
  • 1
    Does anyone know the random number seed used in the Netherlands lotto draw? It'd really help me out:) – Brett Hale Oct 03 '11 at 16:16

5 Answers5

8

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));
Blagovest Buyukliev
  • 42,498
  • 14
  • 94
  • 130
1

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.

Doug T.
  • 64,223
  • 27
  • 138
  • 202
0

time returns a time_t object.

srand is expecting an unsigned int.

crashmstr
  • 28,043
  • 9
  • 61
  • 79
0
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.

David Schwartz
  • 179,497
  • 17
  • 214
  • 278
0
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

Community
  • 1
  • 1
RvdK
  • 19,580
  • 4
  • 64
  • 107