I know that the rand() function can produce random integers, but I want to produce non-integers in the range of 0 to 1. Is there a function that will do this? Thanks.
I know that the rand() function can produce random integers, but I want to produce non-integers in the range of 0 to 1. Is there a function that will do this? Thanks.
FAQ > Prelude's Corner > Random NumbersA few other useful operations are obtaining a random number between 0 and 1 where [0..1) or [0..1]. This can be done very easily using what we learned in the above code:Code:// Range [0..1) double r0 = (double)rand() / ( RAND_MAX + 1 ); // Range [0..1] double r1 = (double)rand() / RAND_MAX;
Last edited by Dave_Sinkula; 01-06-2005 at 02:37 PM. Reason: Added quote.
7. It is easier to write an incorrect program than understand a correct one.
40. There are two ways to write error-free programs; only the third one works.*
Note that if you use the time to seed the random number generator, you may want to skip past the first number. I noticed on mine that the first number was always above 0.95, but after that it is sufficently random.
Also note that this method will only produce RAND_MAX + 1 different doubles. There are plenty of doubles that this method will not produce.
If I did your homework for you, then you might pass your class without learning how to write a program like this. Then you might graduate and get your degree without learning how to write a program like this. You might become a professional programmer without knowing how to write a program like this. Someday you might work on a project with me without knowing how to write a program like this. Then I would have to do you serious bodily harm. - Jack Klein
I have no problems with this code seeded with time causing the first number to be over .95.
Quzah.
Hope is the first step on the road to disappointment.
Thanks for all of your help by the way, your suggestion worked. Also, same here, my first number wasn't always 0.95 or above.
Last edited by scrub05; 01-06-2005 at 04:34 PM.
The 0.95 was just an example. Consider the following:This code produced the following output:Code:#include <ctime> #include <stdlib.h> #include <iostream> #include <windows.h> using std::endl; using std::cout; int main(int argc, char **argv) { srand(time(NULL)); cout << ((double)rand() / RAND_MAX) << endl; Sleep(1000); srand(time(NULL)); cout << ((double)rand() / RAND_MAX) << endl; Sleep(1000); srand(time(NULL)); cout << ((double)rand() / RAND_MAX) << endl; Sleep(1000); srand(time(NULL)); cout << ((double)rand() / RAND_MAX) << endl; Sleep(1000); srand(time(NULL)); cout << ((double)rand() / RAND_MAX) << endl; return 0; }I realize that you (probably) don't want to seed your generator multiple times. However, a shrewd person may realize that the first random number will always be slightly larger than the one before it. For instance, I can predict that in 104 1/2 minutes, I'll be back around 0.9 again. Exploiting that kind of predictability should be simple. Skipping the first "random" number helps to avoid this problem.Code:0.277657 0.277779 0.277871 0.277963 0.278054This code produced this output:Code:include <ctime> #include <stdlib.h> #include <iostream> #include <windows.h> using std::endl; using std::cout; int main(int argc, char **argv) { srand(time(NULL)); rand(); cout << ((double)rand() / RAND_MAX) << endl; Sleep(1000); srand(time(NULL)); rand(); cout << ((double)rand() / RAND_MAX) << endl; Sleep(1000); srand(time(NULL)); rand(); cout << ((double)rand() / RAND_MAX) << endl; Sleep(1000); srand(time(NULL)); rand(); cout << ((double)rand() / RAND_MAX) << endl; Sleep(1000); srand(time(NULL)); rand(); cout << ((double)rand() / RAND_MAX) << endl; Sleep(1000); return 0; }Still not wonderful, but better than the simple linear progression.Code:0.687948 0.0159307 0.343944 0.671987 1
If I did your homework for you, then you might pass your class without learning how to write a program like this. Then you might graduate and get your degree without learning how to write a program like this. You might become a professional programmer without knowing how to write a program like this. Someday you might work on a project with me without knowing how to write a program like this. Then I would have to do you serious bodily harm. - Jack Klein