Random number generation

This is a discussion on Random number generation within the C++ Programming forums, part of the General Programming Boards category; I know that the rand() function can produce random integers, but I want to produce non -integers in the range ...

1. Random number generation

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.

2. FAQ > Prelude's Corner > Random Numbers
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;```

3. 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.

4. I have no problems with this code seeded with time causing the first number to be over .95.

Quzah.

5. 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.

6. The 0.95 was just an example. Consider the following:
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;
}```
This code produced the following output:
Code:
```0.277657
0.277779
0.277871
0.277963
0.278054```
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:
```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;
}```
This code produced this output:
Code:
```0.687948
0.0159307
0.343944
0.671987
1```
Still not wonderful, but better than the simple linear progression.

Popular pages Recent additions