| | |
C++ question and some guidance
Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved |
•
•
Join Date: Jul 2005
Posts: 24
Reputation:
Solved Threads: 0
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.
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.
•
•
Join Date: Jul 2005
Posts: 24
Reputation:
Solved Threads: 0
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.
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/ArnaudRe...2.13.html#rand
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/ArnaudRe...2.13.html#rand
All my posts may be redistributed under the GNU Free Documentation License.
![]() |
Similar Threads
- Writing to an Access Database (Visual Basic 4 / 5 / 6)
- Need guidance as to where to post question (Windows Software)
- Newbie e-mail functionality question (MS Access and FileMaker Pro)
- Algorithms Question (Bisection Method & Sqrt) (Computer Science)
- Ive got a question that needsa answerin. (Search Engine Optimization)
- Context-sensitive grammar question :( (Computer Science)
- Welcome PC Mod Kingdom peeps! (Geeks' Lounge)
- Laptop LCD built into a car? (Monitors, Displays and Video Cards)
- Changing Network Configuration (*nix Software)
Other Threads in the C++ Forum
- Previous Thread: Printf in C++
- Next Thread: How do I use an array of structures?
| Thread Tools | Search this Thread |
Tag cloud for C++
api application array arrays based beginner binary bmp c++ c/c++ calculator char char* class classes code compile compiler console conversion convert count data delete deploy dll download dynamic dynamiccharacterarray encryption error file format forms fstream function functions game givemetehcodez google graph gui homeworkhelp iamthwee ifstream input int java lib library linker list loop looping loops map math matrix memory microsoft newbie news number numbertoword output pointer problem program programming project python random read recursion recursive reference rpg simple sorting string strings temperature template templates test text text-file tree url variable vector video visual visualstudio void win32 windows winsock wordfrequency wxwidgets






