[RESOLVED] RandomNumber Generator Problem

Started by
3 comments, last by ........................................ 17 years, 11 months ago
Hello to whoever reads this, Im creating a Random Number Game, where I generate a random number and the user has to guess it. (probably didn't need to put that explanation) I know how to generate a random number and all that but im getting a weird warning when I build the solution. Im using Visual C++ 2005 Express Edition. This game is a console app. When I build the solution I get these two warning: (38) : warning C4244: 'argument' : conversion from 'time_t' to 'unsigned int', possible loss of data (45) : warning C4244: 'argument' : conversion from 'time_t' to 'unsigned int', possible loss of data On line 38 and 45 in my code is the same thing: srand(time(0)); //seed random number generator based on current time In two C++ books I have it says that this line works...and it does. I can generate a random number and all that. But I don't know why im getting this warning. Though my game works, i'd rather like to figure out how to get rid of these two warnings. If anyone has any ideas, please post. I would appriciate it. Thanks. p.s. At the start of the program I have these loaded up: #include <stdlib.h> #include <time.h> I don't know if this is relevent, but I figured I should tell you all. [Edited by - Koolchamp on April 29, 2006 6:43:27 PM]
Cheers,Ken(Koolchamp)_____________________________"Choose a job you love, and you will never have to work a day in your life." - Confucius"If you don't have a game industry job and you want to go to E3, do what everyone else does. Launch a game review website and call yourself press." - Mike McShaffry(This is true for me) “…..I'm a geek and jocks are my natural enemy.” – Li C. Kuo
Advertisement
Not positive, but I am pretty it's because the max value of time_t is larger than that of an unsigned int so if your time_t happens to be bigger than the max value an unsigned int can hold, it will be clamped to the range of an unsigned int during the conversion, hence the possible loss of data.

-AJ
V/R,-AJThere are 10 kinds of people in the world: Those who understand binary and those who don't...
try
srand((unsigned int)time(0));
the warning comes to show you that the the conversion can cause a loss of data which is irrelevant if you need random numbers.
philipptr:

Thanks man. It got rid of the warnings. I appreciate the hasty answer. Thanks again.
Cheers,Ken(Koolchamp)_____________________________"Choose a job you love, and you will never have to work a day in your life." - Confucius"If you don't have a game industry job and you want to go to E3, do what everyone else does. Launch a game review website and call yourself press." - Mike McShaffry(This is true for me) “…..I'm a geek and jocks are my natural enemy.” – Li C. Kuo
sorry for the short explanation, it was very late in the night and I was almost sleeping on my keaboard :D
with the warning, the compiler shows you that you're giving data of the wrong type to the function, which you might really want to know, because this can be very important when you load data (and want to avoid buffer overflows).
so srand takes an unsigned int. it doesnt make any difference because even if there where some bits lost, you still only want to pass a number to the random seed generator, which does no important things with it. the only thing you would want to avoid is passing always the same number, which wont happen just through this simple casting.
when you write the (unsigned int), you give the command to cast the data, which lets the compiler know that you know about the casting needed to run this piece of code.

This topic is closed to new replies.

Advertisement