Hello,

I've been working on this random number generator code and got it to work using integer outputs, however I need scientific notation. I read on a site that I can use %E for scientific notation, however I get wierd results that aren't really random, and results that are extremely small like 4.880590E-313 (This is the form I need) when I use it. In addition to this, the output numbers that are on the same line don't seem to change value as they did when they were integers. Can someone please help me, I'm really stuck, I have included my working code that displays integers below. Thanks,

-Al

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <cstdlib>
int main ()
{
  
  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"); 
  printf ("JUPITER     m=%d r=20.d0 d=%d\n", rand()%20, rand()%10);
  printf ("%d %d %d \n", rand()%10, rand()%10, rand()%10);
  printf ("%d %d %d \n", rand()%10, rand()%10, rand()%10);
 // printf ("SATURN      m=%E r=20.d0 d=%d \n", rand()%100, rand()%5);
 // printf ("%E %E %E \n", rand()%10+20, rand()%10+20, rand()%10+20);
 // printf ("%E %E %E \n", rand()%10+20, rand()%10+20, rand()%10+20);
 // printf ("URANUS      m=%E r=20.d0 d=%d \n", rand()%100, rand()%5);
 // printf ("%E %d %E \n", rand()%10+20, rand()%10+20, rand()%10+20);
 // printf ("%E %E %E \n", rand()%10+20, rand()%10+20, rand()%10+20);
 // printf ("NEPTUNE     m=%E r=20.d0 d=%d \n", rand()%100, rand()%5);
 // printf ("%E %E %E \n", rand()%10+20, rand()%10+20, rand()%10+20);
 // printf ("%E %E %E \n", rand()%10+20, rand()%10+20, rand()%10+20);
    

  return 0;

}

<< moderator edit: added [code][/code] tags >>

Dani AI

Generated

Short answer: the weird tiny numbers come from a format/argument mismatch. As pointed out, the %E format requires a double. Passing plain int values (like rand()%10+20) to printf with %E invokes undefined behavior and yields garbage such as the tiny exponent values seen. Keeping the same textual layout is fine — the fix is to pass real double values to the same format strings.

A compact, robust pattern that preserves the existing output lines is to generate doubles and print them with the same printf formats. Example (original code style, not present earlier in the thread):

static double uniform(double lo, double hi)
{
    return lo + (hi - lo) * (rand() / (double)RAND_MAX);
}

/* usage */
double m = uniform(0.0, 100.0);
printf("SATURN      m=%.6E r=20.d0 d=%d\n", m, rand() % 5);
printf("%.6E %.6E %.6E\n", uniform(20.0, 30.0), uniform(20.0, 30.0), uniform(20.0, 30.0));

Troubleshooting and polish:

  • Casting an int to double (e.g. (double)(rand()%10 + 20)) or assigning to a double variable fixes the mismatch while keeping the same format strings.
  • Use precision/width specifiers (for example %.6E or %12.6E) to match the exact numeric formatting the downstream program expects.
  • Compile with format warnings enabled (for example -Wall -Wformat on GCC/Clang) to catch mismatches at build time.
  • For better randomness and range control in C++ projects, consider <random> (e.g. std::mt19937 + std::uniform_real_distribution) instead of rand().

This keeps the file layout unchanged while ensuring printed numbers are valid doubles in scientific notation rather than unpredictable garbage.

Recommended Answers

All 6 Replies

%E expects a double, rand()%10+20 is an int.

I'm still pretty new at c++, that's what I was thinking but I don't know how to fix the problem. Thanks

How about breaking things up into a couple of lines. Perhaps have variables a, b, c declared as double. Calculate their values. Then print a, b, and c.

The thing is that I need the format that it's in, I can't really move things around. The ouput is going to be used by another program and that's the format it expects.

Who said anything about changing the format?

#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"); 
  
  i = rand()%20; j = rand()%10;
  printf ("JUPITER     m=%d r=20.d0 d=%d\n", i, j);
  i = rand()%10; j = rand()%10; k = rand()%10;
  printf ("%d %d %d \n", i, j, k);
  i = rand()%10; j = rand()%10; k = rand()%10;
  printf ("%d %d %d \n", i, j, k);
  
  a = rand()%100; j = rand()%5;
  printf ("SATURN      m=%E r=20.d0 d=%d \n", a, j);
  a = rand()%10+20; b = rand()%10+20, c = rand()%10+20;
  printf ("%E %E %E \n", a, b, c);
  a = rand()%10+20; b = rand()%10+20, c = rand()%10+20;
  printf ("%E %E %E \n", a, b, c);

  a = rand()%100; j = rand()%5;
  printf ("URANUS      m=%E r=20.d0 d=%d \n", a, j);
  a = rand()%10+20; j = rand()%10+20; c = rand()%10+20;
  printf ("%E %d %E \n", a, j, c);
  a = rand()%10+20; j = rand()%10+20; c = rand()%10+20;
  printf ("%E %d %E \n", a, j, c);
  
  a = rand()%100; j = rand()%5;
  printf ("NEPTUNE     m=%E r=20.d0 d=%d \n", a, j);
  a = rand()%10+20; j = rand()%10+20; c = rand()%10+20;
  printf ("%E %d %E \n", a, j, c);
  a = rand()%10+20; j = rand()%10+20; c = rand()%10+20;
  printf ("%E %d %E \n", a, j, c);

  return 0;

}

/* my output
)O+_06 Big-body initial data  (WARNING: Do not delete this line!!
)) Lines beginning with `)' are ignored.
)---------------------------------------------------------------------
 style (Cartesian, Asteroidal, Cometary) = Cartesian
 epoch (in days) = 2451000.5
)---------------------------------------------------------------------
JUPITER     m=0 r=20.d0 d=8
1 9 2 
3 0 4 
SATURN      m=3.900000E+01 r=20.d0 d=1 
2.400000E+01 2.100000E+01 2.400000E+01 
2.300000E+01 2.500000E+01 2.400000E+01 
URANUS      m=6.900000E+01 r=20.d0 d=0 
2.400000E+01 29 2.200000E+01 
2.800000E+01 22 2.700000E+01 
NEPTUNE     m=4.600000E+01 r=20.d0 d=4 
2.500000E+01 22 2.300000E+01 
2.700000E+01 22 2.200000E+01 
*/

Thank you so much, now I can continue modifying the code. I really appreciate your willingness to help me out.

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.