Generating random number in a given range in Fortran 77

12,099

Solution 1

Knuth has released into the public domain sources in both C and FORTRAN for the pseudo-random number generator described in section 3.6 of The Art of Computer Programming.

Solution 2

2nd question:

If your file, for example, looks like:

hour temperature pressure humidity
00   15          101325   60
01   15          101325   60
... 24 of them, for each hour one

this simple program will read it:

implicit none
integer hour, temp, hum
real p
character(80) junkline
open(unit=1, file='name_of_file.dat', status='old')
rewind(1)
read(1,*)junkline
do 10 i=1,24
read(1,*)hour,temp,p,hum
 C   do something here ...
 10  end
close(1)
end

(the indent is a little screwed up, but I don't know how to set it right in this weird environment)

My advice: read up on data types (INTEGER, REAL, CHARACTER), arrays (DIMENSION), input/output (READ, WRITE, OPEN, CLOSE, REWIND), and loops (DO, FOR), and you'll be doing useful stuff in no time.

I never did anything with random numbers, so I cannot help you there, but I think there are some intrinsic functions in fortran for that. I'll check it out, and report tomorrow. As for the 3rd question, I'm not sure what you ment (you don't know how many lines of data you'll be having in a file ? or ?)

Share:
12,099
Admin
Author by

Admin

Updated on June 04, 2022

Comments

  • Admin
    Admin almost 2 years

    I am a beginner trying to do some engineering experiments using fortran 77. I am using Force 2.0 compiler and editor. I have the following queries:

    1. How can I generate a random number between a specified range, e.g. if I need to generate a single random number between 3.0 and 10.0, how can I do that?
    2. How can I use the data from a text file to be called in calculations in my program. e.g I have temperature, pressure and humidity values (hourly values for a day, so total 24 values in each text file).
    3. Do I also need to define in the program how many values are there in the text file?
  • Vladimir F Героям слава
    Vladimir F Героям слава about 5 years
    What is the quality of this generator? Which established tests does it or does it not pass succesfully?
  • Vladimir F Героям слава
    Vladimir F Героям слава about 5 years
    That is not very assuring. What does "pretty pseudorandom" mean? softwareengineering.stackexchange.com/questions/147134/… en.m.wikipedia.org/wiki/Diehard_tests
  • Hender
    Hender about 5 years
    Yes, I remember to have been used ENT to test the sieries that was almost 15 years ago or something similar.