Link to home
Start Free TrialLog in
Avatar of A G
A GFlag for United States of America

asked on

Randomly Filling an Array from a text file

First, I am new to Fortran.

I have a text file which has 5000 values .
I want to select 1000 of these numbers randomly and fill an array with it.

How would I do that?

Also: I couldn't find the fortran zone in EE.
Avatar of Hugh McCurdy
Hugh McCurdy
Flag of United States of America image

Maybe you should edit your header so that it includes the word FORTRAN.

Why are you using FORTRAN?  I wrote some programs in FORTRAN but it was so long ago that I don't remember it except to say that new software shouldn't be written in FORTRAN.  I can understand the problem of legacy code maintenance.

I can answer generally.  You'll need a (psuedo) random number generator.  Do you have one?

Are you supposed to print out the 1000 numbers or save them to a file or what?

Are numbers placed supposed to be unique?  That is, if you select the number in location 10 once, may you or may you not select the number in location 10 again?
Avatar of A G

ASKER

I am doing a simulation which generates more than 2 million results per simulation. So if I ran it 1000 times it is going to be much more than that.
Excel is way to slow to do the calculations. Matlab is slightly faster. I read that fortran is simple, hard to work with, but faster than excel and matlab. So thats why I want to give fortran a shot. My only goal is to solve lots of calculations faster.

I dont have a number generator, and I am assuming fortran does not have a rnd() kind of function. I want it to be non repetitive.
Avatar of TommySzalapski
I would highly recommend you use C++ then instead of fortran. It's the same speed or faster for most things and many people (here and elsewhere) know how to use it quite well.
The rest of hmccurdy's questions about how to select the 1000 numbers still do need to be answered though.
Well written C is fast.  C has been described by some as "a high level assembler."  Others will disagree  Regardless, well written C code is hard to beat vs the time it would take to write similar code in assembler.

My suggestion is you give C a try.  You'll need a better program description especially since you are concerned about speed.  Experts can't evaluate things they don't know about.  Is it really 5000 values or could it be 5 million or 5 trillion?  This changes the design if we are worried about speed.  What is the nature of the 5000 numbers?  (It's not 2, 4, 6, 8...10,000 right?)

Another idea is to verify that others agree with me.  I don't know how many points you have to spend, but if posted a question asking about choosing a programming language for simulation speed, you'll get some opinions.  My opinion is to use C.  C++ might be OK but I don't have an opinion on that one way or the other.

OK, I didn't have an opinion on C++ speed but Tommy does and I have reason to believe he correct and I also wonder where my brain was now that I read his post.

C++ is object oriented.  Simulations simulate objects!  C++ is a perfect fit.  Use C++.

Have you seen my brain?  Good that Tommy showed up.  Now it's clear.  Use C++.
C++ is basically just C with more options. If you take C++ and write simple code that doesn't use any classes or polymorphism or anything, then it's essentially identical to C (especially since your compiler contains an optimizer that can make your code even more efficient).
John, are you willing to give C++ a shot?  If so, my guess is you'll need help.  If so, what's your background and what operating system are you using?

Tommy, that is correct.  My experience with C++ is recent.  My experience with C is extensive.  I commented based on where my confidence lies.  However, have worked on writing a simulator but it was in Java.  The concepts are still the same.  

Unless there's something odd about this simulator, I think an OOP is desirable and C++ (if well written) should out perform Java.
Avatar of A G

ASKER

Tommy and hmccurdy,
Thanks for your responses. I am willing to give C++ a shot. The last time I worked with C++, it was 9 years ago, so I'll have to refresh knowledge. My first question would be which compiler or programming environment to use. I remember I used to work with borland. Do you know any free good ones on the internet or do I have to pay?
If you are in Windows (which is sounds like), I've heard good things about DevC++     http://www.bloodshed.net/devcpp.html

Caveat, I haven't actually used it even though I've use MinGW and have used Cygwin in the distant past.  I use the GNU g++ compiler in Linux but that doesn't mean you should.  

A good reference cite is    http://www.cplusplus.com/

And of course, there's Experts Exchange too.  You'll find several people who can give you C/C++ advice here at EE.  There is a C++ zone.  I suggest using both the C++ zone and the C zone for future questions.  You can add a 3rd zone if you can find one that makes sense to you.

Unless it's a secret, you might want to share more about your simulator at some point.  My guess is that it's a natural OOP problem but I can't tell yet.
In addition to the above suggestions.

Eclipse has an IDE for c++
http://eclipse.org/downloads/moreinfo/c.php#
but you need to get your own compiler for it to interface with. minGW would be a good one for that.

Microsoft has a free version of their compiler suite called Visual C++ Express.
http://www.microsoft.com/visualstudio/en-us/products/2010-editions/visual-cpp-express
You would select a C++ console application and create an empty project to avoid extra junk being added.

I know both C and C++ but don't frequent those zones very often. If you tag Misc Programming or Algorithms as the third zone, I'll see it. Also, if you hit 'Ask a related question' after closing one, all the experts in the first question will be notified about the new one (and the new one will have a link back to the first one so the new experts can get on the same page).
Algorithms looks like a good zone.  Perhaps I should start monitoring it.
ASKER CERTIFIED SOLUTION
Avatar of dcesari
dcesari
Flag of Italy image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Before you decide which language to use think about the algorithm itself. Do you want to read the whole file into memory? 5K is not a big deal now (unless you are working for some kind of embedded system, or 5K is not the final number). Then you can get random numbers out of this array with permutation vector. In addition, you won't have to reread the file again in case you need to run several iterations.

Since you've mentioned MATLAB, the code there will take only few lines:
x = importdata('test5000.txt');
iperm = randperm(5000);
y = x(iperm(1:1000));

Open in new window