I want to get random float numbers from 0 to 2 (included). How do I do this?
I want to get random float numbers from 0 to 2 (included). How do I do this?
You could try reading the FAQ. Or maybe type that in the search box and read one of the countless posts on the topic.
Quzah.
Hope is the first step on the road to disappointment.
num=(rand()%3) == 2 ? 2.0 : (rand()%2) + (float)(rand()%1000000)/1000000;
Did you read the FAQ, How can I generate floating-point random numbers? (Like Quzah suggested)? You could also read this article on rand, which is a more general treatment of the function, as opposed to using it with floating point numbers.
Last edited by kermit; 05-15-2010 at 07:11 PM.