As mentioned, the srand call needs to moved out of that function and into main so it is called once.
You also have a problem with how you are using the return from rand(). You stated you want random values between 0 and 4. The function returns random values from 0 to RAND_MAX with is typically 32K. You are adding one to the result of the return modulo 4. This means the values will range from 1 to 4. Here is a function that will return values within the range specified.
int GetRandomValue( int minval, int maxval )
{
int range = maxval - minval + 1;
int rval = rand() % range;
return rval + minval;
}
You want numbers that range from 0 to 4 so call this function and pass the values 0 and 4 to it.
Here's code that will test of that function :
void DoRandomValueTest()
{
std::vector< int > values( 10, 0 );
const int count = 1000;
for( int n = 0; n < count; ++n )
{
int rv = GetRandomValue( 1, 8 );
values[ rv ] += 1;
}
size_t valueCount = values.size();
trace( _T( "random value vector has %d items\n" ), (int) valueCount );
for( size_t i = 0; i < valueCount; ++i )
trace( _T( "%2d : %4d times\n" ), (int) i, values[ i ] );
}
Here is the output from the test :
21:04:02.279 TestThread_07 random value vector has 10 items
21:04:02.279 TestThread_07 0 : 0 times
21:04:02.279 TestThread_07 1 : 129 times
21:04:02.279 TestThread_07 2 : 143 times
21:04:02.279 TestThread_07 3 : 116 times
21:04:02.279 TestThread_07 4 : 113 times
21:04:02.279 TestThread_07 5 : 119 times
21:04:02.279 TestThread_07 6 : 135 times
21:04:02.279 TestThread_07 7 : 129 times
21:04:02.279 TestThread_07 8 : 116 times
21:04:02.279 TestThread_07 9 : 0 times
As you can see, all values are within the range of 1 to 8. The distribution is reasonably flat for such a small range.