I've been working on this code which generates random numbers, however I need it to generate random numbers between 5.15053396E-5 and 0.013365. Is there a way to define rand() to only generate random numbers within this range? What I tried to do was to generate a number between 110607 - 2.86E7 and divide by RAND_MAX by using rand()%2.84864E7+0.013365/RAND_MAX and I got an error about 'int' and 'double'. I'm not well versed in c++ so I'm not sure of the most direct method in doing this. Here's the sc

``````#include <stdio.h>
#include <stdlib.h>
#include <time.h>

int main ()
{
double a, b, c;
int i, j, k;
srand ( time(NULL) );

printf (")O+_06 Big-body initial data  (WARNING: Do not delete this line!!\n)");
printf (") Lines beginning with `)' are ignored.\n");
printf (")---------------------------------------------------------------------\n");
printf (" style (Cartesian, Asteroidal, Cometary) = Cartesian\n");
printf (" epoch (in days) = 2451000.5\n");
printf (")---------------------------------------------------------------------\n");

a = rand()%2.84864E7+0.013365/RAND_MAX; b = rand ()%20 + (float) rand()/RAND_MAX;
//mass (m) needs to be between 5.15053396E-5 and 0.013365
printf ("JUPITER     m=%.17E r=20.d0 d=%.2f\n", a, b);
a = rand ()%20 + (float) rand()/RAND_MAX; b = rand ()%20 + (float) rand()/RAND_MAX; c = rand ()%20 + (float) rand()/RAND_MAX;
printf ("%.17E %.17E %.17E \n", a, b, c);
a = rand ()%20 + (float) rand()/RAND_MAX; b = rand ()%20 + (float) rand()/RAND_MAX; c = rand ()%20 + (float) rand()/RAND_MAX;
printf ("%.17E %.17E %.17E \n", a, b, c);
printf ("0.0.0 \n");
// a = rand ()%20 + (float) rand()/RAND_MAX; b = rand ()%20 + (float) rand()/RAND_MAX;
//printf ("SATURN      m=%E r=20.d0 d=%.2f \n", a, b);
//a = rand ()%20 + (float) rand()/RAND_MAX; b = rand ()%20 + (float) rand()/RAND_MAX; c = rand ()%20 + (float) rand()/RAND_MAX;
//printf ("%.17E %.17E %.17E \n", a, b, c);
//a = rand ()%20 + (float) rand()/RAND_MAX; b = rand ()%20 + (float) rand()/RAND_MAX; c = rand ()%20 + (float) rand()/RAND_MAX;
//printf ("%.17E %.17E %.17E \n", a, b, c);
//printf ("0.0.0 \n");
//a = rand ()%20 + (float) rand()/RAND_MAX; b = rand ()%20 + (float) rand()/RAND_MAX;
//printf ("URANUS      m=%E r=20.d0 d=%.2f \n", a, b);
//a = rand ()%20 + (float) rand()/RAND_MAX; b = rand ()%20 + (float) rand()/RAND_MAX; c = rand ()%20 + (float) rand()/RAND_MAX;
//printf ("%.17E %.17E %.17E \n", a, b, c);
//a = rand ()%20 + (float) rand()/RAND_MAX; b = rand ()%20 + (float) rand()/RAND_MAX; c = rand ()%20 + (float) rand()/RAND_MAX;
//printf ("%.17E %.17E %.17E \n", a, b, c);
//printf ("0.0.0 \n");
//a = rand ()%20 + (float) rand()/RAND_MAX; b = rand ()%20 + (float) rand()/RAND_MAX;
//printf ("NEPTUNE     m=%E r=20.d0 d=%.2f \n", a, b);
//a = rand ()%20 + (float) rand()/RAND_MAX; b = rand ()%20 + (float) rand()/RAND_MAX; c = rand ()%20 + (float) rand()/RAND_MAX;
//printf ("%.17E %.17E %.17E \n", a, b, c);
//a = rand ()%20 + (float) rand()/RAND_MAX; b = rand ()%20 + (float) rand()/RAND_MAX; c = rand ()%20 + (float) rand()/RAND_MAX;
//printf ("%.17E %.17E %.17E \n", a, b, c);
//printf ("0.0.0 \n");

return 0;

}``````

Thanks

This would be a good page to read, along with this one. That should cover all of the bases to help you solve your problem. :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.18 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.