Generating 3 random numbers

Hi,

I want to generate 3 random numbers by calling a function which generates a number 3 times. The function is:

1
2
3
4
5
6
7
8
9
int randomgetal()
{
	int getal;
	srand(time(0));

	getal = 2+ rand() % 6;

	return getal;
}


Very easy. First of all, can anyone explain me the meaning of (0) in the time() function in there? I got it from a tutorial. Also, the compiler gives me this warning:
warning C4244: 'argument' : conversion from 'time_t' to 'unsigned int', possible loss of data

Why is that and is there a way to solve it?

Now my real problem... just calling this function 3 times doesn't work because it has the same system time and so generates 3 times the same number. What's the best way to let it generate 3 random numbers (they may be the same)?
Last edited on
time(0) '0' means current time
warning C4244: that shouldn't create problems

Same numbers: You should call srand(time(0)) only once in your program. Move it outside the function, a good place is at the beginning of main
Hi Bazzy

Thank you very much - I understand how it works now =)
Bazzy spoke too quickly here. The 0 in time(0) is actually a null pointer. time() will write its result to the given address as well as return it if this pointer is non-null.
Bazzy spoke too quickly here. The 0 in time(0) is actually a null pointer.
Did he say otherwise?
Yes. He said
'0' means current time
This is wrong since it always gives the current time, whether or not the pointer is null.
Topic archived. No new replies allowed.