Thursday, July 9, 2009

How to create a random number between 0 and 1?

Hi,





I am coding in C++. How do I create a random number between 0 and 1 ? e.g 0.75, 0.80, 0.1 etc

How to create a random number between 0 and 1?
Hmmm.... may I suggest that you generate a counting number between 1 and 100 and then divide the resulting random integer by one hundred, casting to a float as needed, of course. This will give a fair approximation of a true random decimal between 0 and 1, I think.
Reply:Use the rand() func. Then divide by the value RAND_MAX.

sending flowers

No comments:

Post a Comment