Hello,
I made a program that uses a random number generator,
my generator looks like this:

void randomize ()
{
srand((unsigned)time(NULL));
}

int random (int highest)
{
	
	int randomnum;
	
	randomnum= int(highest*rand()/(RAND_MAX+1.0));
	return randomnum;
}

here is the piece of code that calls the generator:

int  first,second;
randomize();
first=random(8);
second=random(11);

the weird thing is that the second call works well and gives a random number every time,but the first doesn't.
It gives the same number every time,and that number always equals
the function parameter(int highest) minus 1(when the parameter is 8
the function returns 7,when I changed it to 10 the function returned 9 etc.).
Can anyone explain why does it happen?

p.s.
I'm using VC++ 6.0

Hello Salem,
I know that rand() is a pseudorandom.
You probably missed this part of my program:

void randomize ()
{
srand((unsigned)time(NULL));
}
.
.
.
.
randomize();

as you see I seed the generator with current time,therfore
the generator should start with a different number every time,
and the problem I described shouldn't happen.

Hello Salem,
I know that rand() is a pseudorandom.
You probably missed this part of my program:

void randomize ()
{
srand((unsigned)time(NULL));
}
.
.
.
.
randomize();

as you see I seed the generator with current time,therfore
the generator should start with a different number every time,
and the problem I described shouldn't happen.

You shouldn't call srand more than once in the lifetime of your program. Perhaps this is why you're getting repetition?

Also, time isn't exactly the best seed for srand. Its common for the first number to always be the same when seeding with the current time. you could try a different method of seeding the generator, such as the one suggested by Narue here -
http://www.eternallyconfuzzled.com/arts/jsw_art_rand.aspx

> the generator should start with a different number every time,
And if the implementation of rand() just uses the most significant 16 bits of the 32 bit number, that as far as it is concerned, time() is a constant pretty much throughout the day.

So it can take a couple of iterations for all the rapidly changing least significant bits to have an impact on the pseudo random sequence.

If you really want to know, run the code in the debugger and then single step into the srand() and rand() calls in asm mode and figure out what it is doing.

Thanks again for all the help,
just wanted to ask one more question:
In the link that Bench posted(http://www.eternallyconfuzzled.com/arts/jsw_art_rand.aspx) there is a piece of code that looks like this:

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

I just wanted to ask what meaning the "U" after the "2" has?
(I guess it means "unsigned",but why does it matter,why just not write:
UCHAR_MAX+2)

I believe omitting the 'U' from the expression causes a warning to be thrown up when compiling with warnings at their highest level. Otherwise, its not a problem.

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.