There's nothing wrong with that approach, as long as the underlying random number generator works correctly. You will always have slight quantization errors which shift the distribution away from perfect uniformity, even if the base RNG is perfectly distributed, but in most cases it is unimportant.
Originally Posted by indigo0086
However, your particular method:
Could probably be improved. Instead of coding this by hand everywhere, write a single function that returns a floating point value in the range [0.0, 1.0). You can do this easily by dividing the output of rand() by RAND_MAX + 1:
part.size = static_cast<float>(rand() % ((3 - 1) + 1) + 1 ) / 10;
part.dy = static_cast<float>(rand() % ((100 - 25) + 1) + 25 ) / 100;
The cast of RAND_MAX to float is important. You can probably figure out why. Using this you can easily get numbers in any given range:
return float(rand()) / (float(RAND_MAX) + 1.0);
Note that you'll never get exactly 1.0 as a result from random_unity(), and accordingly you'll never get exactly "max" from random_in_range(). If you want the range to be fully inclusive, i.e. [0.0, 1.0] instead of [0.0, 1.0), drop the "+ 1" from the denominator in random_unity().
float random_in_range(float min, float max)
return random_unity() * (max - min) + min;