Question Programming a random number generator to use scientific notation

vonneuman

Orbinaut
Joined
Nov 8, 2009
Messages
254
Reaction score
1
Points
0
Location
Missouri S&T
thomas suggested in his randomized planet exploration thread that you could have a "random solarsystem generator". So I have started working on one, Its not as hard as you would think. I am using random number generators to determine the parameters and to choose from modules already installed. Then it will simply export the information in a file.
However I have hit a snag. A random number generator produces numbers that look like this: 351864476. In order for it to work in orbiter it has to look more like this:4.797e2. For stuff like mass and size. My question is how do I get c++ to get numbers into scientific notation?
 

Hielor

Defender of Truth
Donator
Beta Tester
Joined
May 30, 2008
Messages
5,580
Reaction score
2
Points
0
thomas suggested in his randomized planet exploration thread that you could have a "random solarsystem generator". So I have started working on one, Its not as hard as you would think. I am using random number generators to determine the parameters and to choose from modules already installed. Then it will simply export the information in a file.
However I have hit a snag. A random number generator produces numbers that look like this: 351864476. In order for it to work in orbiter it has to look more like this:4.797e2. For stuff like mass and size. My question is how do I get c++ to get numbers into scientific notation?
Read the documentation for whatever method you're using to output the data.

---------- Post added at 00:23 ---------- Previous post was at 00:20 ----------

Also, there are already a couple programs which do what you're asking. There's a couple threads floating around the forums regarding the "Orbiter Galaxy" project, search for that.
 

computerex

Addon Developer
Addon Developer
Joined
Oct 16, 2007
Messages
1,282
Reaction score
17
Points
0
Location
Florida
Try looking at this:

Code:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>


double grn(double to) 
{ 
    return rand()/(double(RAND_MAX)+1)*to; 
} 

int main()
{
    srand(time(NULL));
    
    for ( unsigned int i = 0; i < 5000; i++ )
    {
        printf("%e\n", grn(5e6));
    }
    scanf(" ");
    return 0;
}
 

Hielor

Defender of Truth
Donator
Beta Tester
Joined
May 30, 2008
Messages
5,580
Reaction score
2
Points
0
To calculate random numbers between A and B:

RandomNumber = Random * (B-A) + A

where

Random between 0 and 1
Uh, what?

That has absolutely nothing to do with what he asked...
 

Hielor

Defender of Truth
Donator
Beta Tester
Joined
May 30, 2008
Messages
5,580
Reaction score
2
Points
0
If I am correct double precision variables already use scientific notation when needed.
Closer, but still not quite there--he was asking how to force it into scientific notation when it would otherwise not be, and the code that ctex posted (which uses the %e printf token) does so.
 
Top