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.
Printable View
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 NumbersQuote:
A 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;
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.
I have no problems with this code seeded with time causing the first number to be over .95.
Quzah.
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.
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.278054
This 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