I am trying to get random numbers between 10 and 20 with decimals, first I take the integer part and then the decimal part, this so that the probabilities of the integer part have a greater possibility of varying, and then the decimal part, but when adding the two parts begins to be rounded, I don't want it to be rounded because I want to get an exact number of decimal places, in this case 7 decimal places.
#include<iostream>
#include<math.h>
double random_decimal(int inicio, int fin, int numero_decimales)
{
double int_part = (rand() % (inicio + 1 - fin) + inicio);
int num = pow(10, numero_decimales);
double decimal_part = (double)(rand() % (1 - num)) / num;
cout << "Int: "<< int_part << " Dec: "<< decimal_part << " Sum: "<< int_part + decimal_part << endl;
return (double)(int_part + decimal_part);
}
int main()
{
int ed;
double ps, estat;
for(int i = 0; i < 10; i++)
{
double x = random_decimal(10, 20, 7);
}
}