I use this function to generate random values :
template< typename T >
inline T GetRandomValue( T minval, T maxval )
{
return ( rand() * ( maxval - minval ) / (T) RAND_MAX ) + minval;
}
Converting that to a non-template function, it can look like this :
inline float GetRandomValue( float minval, float maxval )
{
return ( rand() * ( maxval - minval ) / (float) RAND_MAX ) + minval;
}
then you can call it like this :
float value = GetRandomValue( 0.0f, 10.0f );
Remember to seed the PRNG with
srand
. Here is one way :
short seed = short( time(NULL) % RAND_MAX );
srand( seed );
for a unique sequence almost every time. Seed it with the same value for a repeatable sequence, preferably a prime number like 43.