Possible Duplicate:
How do you generate a random double uniformly distributed between 0 and 1 from C++?
I’m trying to figure out how to generate a random number between 0 and pi, but the usual method won’t work because mod does integer division:
double num = rand() % 2*M_PI;
How would I go about doing this?
Thanks.
Generate a random number between 0 and 1, and then multiply the result by
2*M_PI.If you have a uniform distribution between 0 and 1, you will also have a uniform distribution between 0 and
2*M_PI, to the limit of precision available in the numeric type you are using.For generating a random uniform double between 0 and 1, see the answer suggested by @dasblinkenlight in his comment.