How do you create a random double between .1 and 2.5?
This is what I have been trying.
srand(time(NULL));
((double) rand() / RAND_MAX) * 2.5 + .1
How do you create a random double between .1 and 2.5?
This is what I have been trying.
srand(time(NULL));
((double) rand() / RAND_MAX) * 2.5 + .1
The modern way is to use a random number distribution:
#include <iostream>
#include <random>
using namespace std;
int main()
{
random_device rd;
mt19937_64 generator(rd());
uniform_real_distribution<double> distribution(0.1, 2.5);
for (int i = 0; i < 10; i++)
{
double d = distribution(generator);
cout << d << endl;
}
return 0;
}
Note that 0.1 <= d < 2.5
.