Hello all,

I haven't used c++ for years and now I need it for school. I am running a fortran 77 code and I wanted to know if it is possible to write a program in c++ (That's the only language I know) that generates random numbers with decimals and creates a file with the output in the form below. Where the bold letters are each separate random numbers in a certain range.


JUPITER m=X r=3.d0 d=depends on X
Y U O
A S W

0. 0. 0.
SATURN m=V r=3.d0 d=depends on V
M C Q
D N P
0. 0. 0.

I don't have my C++ books anymore so I don't have a resource to look at other than the internet, and I don't know if this is very ambitious. Any input would be great, thank you for you time. :)

Can you be more specific?

I guess what I'm asking is I'm not sure how to have a random number generator that places the values it comes up with in the places where the bold letters are in my previous message. From the random number generators I have seen you compile them and they create an executable that has the random numbers, what I need is a file that is created with the random numbers already places where the bold letters are. Ultimately this file will be used by the fortran code and this is what it looks like as it is...

JUPITER m=9.54791938424326609E-04 r=3.d0 d=1.33
4.84143144246472090E+00 -1.16032004402742839E+00 -1.03622044471123109E-01
1.66007664274403694E-03 7.69901118419740425E-03 -6.90460016972063023E-05

0. 0. 0.
SATURN m=2.85885980666130812E-04 r=3.d0 d=0.70
8.34336671824457987E+00 4.12479856412430479E+00 -4.03523417114321381E-01
-2.76742510726862411E-03 4.99852801234917238E-03 2.30417297573763929E-05

0. 0. 0.

but instead of using these bold values, I need random values, and need them written out to a file similar to that above. I hope I have made myself clear. Thanks.

Use #include <cstdlib> at the top of your program, and then use srand once to seed the random number generator. Then use rand() to produce a random number between zero and RAND_MAX. Use some math to scale the random number to the range you want.

You might want to try to get larger initial random integers by using (rand() * RAND_MAX + 1)) + rand() and scale those numbers to the range of doubles that you want.

Then use cout with setprecision to output the values. Here's a link to the functions described in a reference.

http://www.fortran-2000.com/ArnaudRecipes/Cstd/2.13.html#rand

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.