C++ question and some guidance

Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Jul 2005
Posts: 24
Reputation: altheastronut is an unknown quantity at this point 
Solved Threads: 0
altheastronut altheastronut is offline Offline
Newbie Poster

C++ question and some guidance

 
0
  #1
Jul 27th, 2005
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.
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,802
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 747
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: C++ question and some guidance

 
0
  #2
Jul 27th, 2005
Can you be more specific?
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 24
Reputation: altheastronut is an unknown quantity at this point 
Solved Threads: 0
altheastronut altheastronut is offline Offline
Newbie Poster

Re: C++ question and some guidance

 
0
  #3
Jul 27th, 2005
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.
Reply With Quote Quick reply to this message  
Join Date: Jun 2005
Posts: 2,052
Reputation: Rashakil Fol is just really nice Rashakil Fol is just really nice Rashakil Fol is just really nice Rashakil Fol is just really nice 
Solved Threads: 139
Team Colleague
Rashakil Fol's Avatar
Rashakil Fol Rashakil Fol is offline Offline
Super Senior Demiposter

Re: C++ question and some guidance

 
0
  #4
Jul 27th, 2005
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
All my posts may be redistributed under the GNU Free Documentation License.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C++ Forum
Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC