• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 486
  • Last Modified:

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.
0
awesomejohn19
Asked:
awesomejohn19
  • 6
  • 4
  • 2
  • +2
1 Solution
 
Hugh McCurdyCommented:
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?
0
 
awesomejohn19Author Commented:
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.
0
 
TommySzalapskiCommented:
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.
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
TommySzalapskiCommented:
The rest of hmccurdy's questions about how to select the 1000 numbers still do need to be answered though.
0
 
Hugh McCurdyCommented:
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.

0
 
Hugh McCurdyCommented:
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++.
0
 
TommySzalapskiCommented:
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).
0
 
Hugh McCurdyCommented:
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.
0
 
awesomejohn19Author Commented:
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?
0
 
Hugh McCurdyCommented:
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.
0
 
TommySzalapskiCommented:
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).
0
 
Hugh McCurdyCommented:
Algorithms looks like a good zone.  Perhaps I should start monitoring it.
0
 
dcesariCommented:
If after all this you still would like to try fortran, I send you a tentative program to do that, using the random_number() intrinsic routine and remembering the already randomly chosen numbers through a mask. I did not test it so it may even not compile, just take it a s a starting point.

Concerning compilers, I like gfortran, which is part of GNU compiler collection (gcc &co.) available also on windows through MINGW,

PROGRAM randomchoice
RERAL :: rndnum
REAL,ALLOCATABLE :: in(:), out(:)
INTEGER :: n1, n2, nrem

n1=5000
n2=3000
nrem=n1

ALLOCATE(in(n1),mask(n1),out(n2))
mask=(.TRUE.)
READ(*,*)in

DO i = 1, n2
  CALL random_number(rndnum)
! get a random element from the remaining elements of array in
! and update mask of remaining elements
  out(i) = getnth(in, mask, rndnum*REAL(nrem-1) + 1)
  nrem = nrem - 1
ENDDO

CONTAINS

REAL FUNCTION getnth(in, mask, ind)
REAL,INTENT(in) :: in(:)
LOGICAL,INTENT(in) :: mask(:)
INTEGER :: ind
INTEGER :: lind, i

lind=0
DO WHILE(
DO i = 1, SIZE(in)
  IF (mask(i)) THEN
    lind = lind + 1
    IF (lind == ind) THEN
      getnth = in(i)
      mask(i) = .FALSE.
      RETURN
    ENDIF
  ENDIF
ENDDO
! this pint should never be reached
! otherwise handle error condition

END FUNCTION getnth

END PROGRAM randomchoice

Open in new window

0
 
yuk99Commented:
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

0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 6
  • 4
  • 2
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now