Hello ,

My program works fine but I still have one error that appears and I would like to fix it. It is complaining about

srand( time(0) );

with the following error:

.cpp(25) : warning C4244: 'argument' : conversion from 'time_t' to 'unsigned int', possible loss of data

My code is the following:

// contains the function prototypes for
#include <iostream>		
#include <cstdlib>		
#include <iomanip>		
#include <ctime>		
using namespace std;	

#include "Dice.h"		

//constructor to initialize results array
Dice::Dice(const int sumsArray[])
{
	// initialize the elements of the array results to 0
	for(int i = 0; i < possibleSums; i++)
		results[i] = sumsArray[i];	
}

void Dice::rollDice() 
{
	srand( time(0) );	
	for( int i = 0;  i < 36000;  ++i )		
               {
		int sum = 0;			
		sum += rand() % 6 + 1;	
		sum += rand() % 6 + 1;	
		++ results[sum];		
	}
}


void Dice::displayMessage()
{
	cout
	<< "Sum     Count" << endl
	<< "-------------" << endl;


	for( int i = 2;  i <= 12;  ++i )
	{
		cout
		<< setw(3) << i << ": "				
		<< setw(7) << results[i] << endl;
	}
}

Try-- srand( time(NULL) );

Sorry that did not work, same issue

When I did my RNG for my RPG, I had to do

void initialSeed(){
	srand((unsigned)time(0));}

try using time without ctime -

ctime has a function called time_t time ( time_t * timer ) which most likely masks the time function defined in std

>Try--
>srand( time(NULL) );
How is that supposed to fix the incompatibility of time's return value with srand's parameter? By the way, 0 and NULL are identical in C++.

>try using time without ctime
Then the declaration would be missing.

>ctime has a function called time_t time ( time_t * timer )
>which most likely masks the time function defined in std
It's generally a bad idea to talk about things unless you have a clue. Seriously, I can't imagine where you pulled that explanation from.

Here's the real problem. time(0) returns a value of type time_t. srand takes a parameter of type unsigned int. The warning is saying that for the OP's compiler, time_t is a larger integral type than unsigned int and converting from the larger type to the smaller type may affect the value.

The easy solution is to use a cast to explicitly tell the compiler you know what you're doing and to shut the hell up:

srand ( (unsigned)time ( 0 ) );

But because of standard restrictions on time_t, you can't be sure that this will work in all cases (though I don't know of any systems where it will fail). The 100% correct solution is not to use time_t directly, but rather a hash of the value for seeding rand:

#include <climits>
#include <cstdlib>
#include <ctime>
#include <iostream>
#include <limits>

unsigned hash_time()
{
  time_t now = time ( 0 );
  unsigned char *p = (unsigned char *)&now;
  unsigned seed = 0;

  for ( size_t i = 0; i < sizeof now; i++ )
    seed = seed * ( UCHAR_MAX + 2U ) + p[i];

  return seed;
}

int main()
{
  srand ( hash_time() );

  for ( int i = 0; i < 10; i++ )
    std::cout<< rand() <<'\n';
}

Or some other suitable transformation.

Odd, I can use time without ctime, which is why I gave the OP the suggestions.

Here's my example--

#include <iostream>

using namespace std;

int main(){
   srand(time(NULL));  
   cout << rand() << endl;
   cin.get();
   return 0;
}

Works fine.

>Works fine.
I'd guess one of the following:

1) iostream includes it somewhere along the line.
2) Your compiler is nice enough not to throw an error.
3) Your compiler is too old to conform to the C++ standard.


Here's the real problem. time(0) returns a value of type time_t. srand takes a parameter of type unsigned int. The warning is saying that for the OP's compiler, time_t is a larger integral type than unsigned int and converting from the larger type to the smaller type may affect the value.

The easy solution is to use a cast to explicitly tell the compiler you know what you're doing and to shut the hell up:

srand ( (unsigned)time ( 0 ) );

Thanks - this worked! The 100% solution makes sense as well but I am a beginner so I kept it simple.

Simply use "srand()" at the start of the program.

commented: Thanks for uselessly repeating what was already stated over a YEAR AGO! -7

Simply use "srand()" at the start of the program.

srand() takes an argument.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.