I was wondering how to generate a random REAL number within an array.
I always generate an error with this equation
n = 20.2 + rand() % 33.3
due to the fact, I believe, that the modulus operator does not work with real numbers.
I was wondering how to generate a random REAL number within an array.
I always generate an error with this equation
n = 20.2 + rand() % 33.3
due to the fact, I believe, that the modulus operator does not work with real numbers.
Last edited by aromash; 11-20-2010 at 04:57 PM.
You're correct. So I gather you're looking for a random number between 20.2 and 53.5? Try something like
Code:float r; r = ((float) rand() / RAND_MAX); // generate a random value between 0 and 1 r *= 33.3; // scale this up to the range you want r += 20.2; // slide the range up to the starting value you want r = 20.2 + 33.3 * ((float) rand() / RAND_MAX); // same thing, in one line
those numbers are just arbitrarily chosen numbers just to prove a point. I'll try that formula out, thanks!