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

Dani AI

Generated

— the compile error and the wrong results come from trying to use the integer modulus operator (%) with floating-point literals and from mixing ints and doubles in the same expression. The simplest correct approach is to make a normalized double r in [0,1) and scale it into your interval:

double value = min + r * (max - min)

Using the old C rand() API, compute r by casting rand() to double before division so you do floating-point division (not integer division). Example (legacy approach):

#include <cstdlib>
#include <ctime>

double uniform_rand(double min, double max) {
    return min + (double)rand() / (RAND_MAX + 1.0) * (max - min);
}

int main() {
    srand((unsigned)time(nullptr));
    double x = uniform_rand(5.15053396e-5, 0.013365);
}

A better, more modern and higher-quality solution is to use C++11's <random>. std::mt19937 plus std::uniform_real_distribution<double> gives much better precision and statistical quality than rand():

#include <random>

std::random_device rd;
std::mt19937 gen(rd());
std::uniform_real_distribution<double> dist(5.15053396e-5, 0.013365);
double x = dist(gen);

Notes and tips: rand()/RAND_MAX is simple but often low precision (typical RAND_MAX is 32767). rand()%N is only for integers and introduces bias; avoid it for real ranges. uniform_real_distribution produces values in [a,b) by default. For background reading see the C++ reference pages on rand and on <random>/uniform_real_distribution (rand, uniform_real_distribution). was right to point to tutorials — the above gives the direct, practical fix.

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 developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.