0

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

2
Contributors
1
Reply
2
Views
12 Years
Discussion Span
Last Post by Narue
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.