Originally Posted by

**indigo0086**
I'm basically creating an openGL program where I have to determine the size, alpha, accelleration, and color of particles, so they range in the less than 1 range. I am doing it by generating a larger random number and dividing it by a factor to reduce it to a fraction of the larger number for example

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.

However, your particular method:

Code:

part.size = static_cast<float>(rand() % ((3 - 1) + 1) + 1 ) / 10;
part.dy = static_cast<float>(rand() % ((100 - 25) + 1) + 25 ) / 100;

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:

Code:

float random_unity(void)
{
return float(rand()) / (float(RAND_MAX) + 1.0);
}

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:

Code:

float random_in_range(float min, float max)
{
return random_unity() * (max - min) + min;
}

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().