Sign in to follow this  
Koolchamp

[RESOLVED] RandomNumber Generator Problem

Recommended Posts

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]

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this