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:
我是初学者,尝试使用fortran 77进行一些工程实验。我使用的是Force 2.0编译器和编辑器。我有以下问题:
- 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?
- 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).
- Do I also need to define in the program how many values are there in the text file?
如何在指定范围之间生成随机数,例如如果我需要生成3.0到10.0之间的单个随机数,我该怎么办?
如何在程序中的计算中使用文本文件中的数据。例如,我有温度,压力和湿度值(一天的每小时值,因此每个文本文件中总共24个值)。
我是否还需要在程序中定义文本文件中有多少值?
4 个解决方案
#1
Fortran 77's standard does not specify a random number generator, but you can use any of the innumerable sources freely provided for that purpose; http://www.cisl.ucar.edu/zine/96/spring/articles/3.random-6.html for example has a good, usable f77 SRAND
subroutine ready for you to copy and paste.
Fortran 77的标准没有指定随机数生成器,但您可以使用为此目的*提供的任何无数源; http://www.cisl.ucar.edu/zine/96/spring/articles/3.random-6.html例如有一个好的,可用的f77 SRAND子程序,可供您复制和粘贴。
#2
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.
Knuth已经在C和FORTRAN的公共域源中发布了“计算机编程艺术”第3.6节中描述的伪随机数生成器。
#3
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.
我的建议:阅读数据类型(INTEGER,REAL,CHARACTER),数组(DIMENSION),输入/输出(READ,WRITE,OPEN,CLOSE,REWIND)和循环(DO,FOR),你会做的很快就会有用的东西。
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 ?)
我从来没有对随机数做任何事情,所以我无法帮助你,但我认为fortran中有一些内在的功能。我会检查出来,并明天报告。至于第三个问题,我不确定你的意思(你不知道你将在一个文件中有多少行数据?或者?)
#4
You'll want to check your compiler manual for the specific random number generator function, but chances are it generates random numbers between 0 and 1. This is easy to handle - you just scale the interval to be the proper width, then shift it to match the proper starting point: i.e. to map r
in [0, 1]
to s
in [a, b]
, use s = r*(b-a) + a
, where r
is the value you got from your random number generator and s
is a random value in the range you want.
您需要检查编译器手册中的特定随机数生成器函数,但有可能生成0到1之间的随机数。这很容易处理 - 您只需将间隔缩放为适当的宽度,然后将其移至匹配正确的起点:即将[0,1]中的r映射到[a,b]中的s,使用s = r *(ba)+ a,其中r是从随机数生成器得到的值和s是您想要的范围内的随机值。
Idigas's answer covers your second question well - read in data using formatted input, then use them as you would any other variable.
Idigas的答案很好地涵盖了你的第二个问题 - 使用格式化输入读取数据,然后像使用任何其他变量一样使用它们。
For your third question, you will need to define how many lines there are in the text file only if you want to do something with all of them - if you're looking at reading the line, processing it, then moving on, you can get by without knowing the number of lines ahead of time. However, if you are looking to store all the values in the file (e.g. having arrays of temperature, humidity, and pressure so you can compute vapor pressure statistics), you'll need to set up storage somehow. Typically in FORTRAN 77, this is done by pre-allocating an array of a size larger than you think you'll need, but this can quickly become problematic. Is there any chance of switching to Fortran 90? The updated version has much better facilities for dealing with standardized dynamic memory allocation, not to mention many other advantages. I would strongly recommend using F90 if at all possible - you will make your life much easier.
对于你的第三个问题,你需要定义文本文件中有多少行,只要你想对所有这些行做一些事情 - 如果你正在阅读这条线,处理它,然后继续,你可以在不知道提前行数的情况下过关。但是,如果您要将所有值存储在文件中(例如,具有温度,湿度和压力的数组,以便您可以计算蒸汽压力统计数据),则需要以某种方式设置存储。通常在FORTRAN 77中,这是通过预先分配大于您认为需要的大小的数组来完成的,但这很快就会成为问题。有没有机会切换到Fortran 90?更新版本具有更好的处理标准化动态内存分配的工具,更不用说许多其他优点。如果可能的话,我强烈建议使用F90 - 你会让生活更轻松。
Another option, depending on the type of processing you're doing, would be to investigate algorithms that use only single passes through data, so you won't need to store everything to compute things like means and standard deviations, for example.
另一种选择,取决于您正在进行的处理类型,将研究仅使用单次传递数据的算法,因此您不需要存储所有内容来计算诸如均值和标准偏差等内容。
#1
Fortran 77's standard does not specify a random number generator, but you can use any of the innumerable sources freely provided for that purpose; http://www.cisl.ucar.edu/zine/96/spring/articles/3.random-6.html for example has a good, usable f77 SRAND
subroutine ready for you to copy and paste.
Fortran 77的标准没有指定随机数生成器,但您可以使用为此目的*提供的任何无数源; http://www.cisl.ucar.edu/zine/96/spring/articles/3.random-6.html例如有一个好的,可用的f77 SRAND子程序,可供您复制和粘贴。
#2
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.
Knuth已经在C和FORTRAN的公共域源中发布了“计算机编程艺术”第3.6节中描述的伪随机数生成器。
#3
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.
我的建议:阅读数据类型(INTEGER,REAL,CHARACTER),数组(DIMENSION),输入/输出(READ,WRITE,OPEN,CLOSE,REWIND)和循环(DO,FOR),你会做的很快就会有用的东西。
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 ?)
我从来没有对随机数做任何事情,所以我无法帮助你,但我认为fortran中有一些内在的功能。我会检查出来,并明天报告。至于第三个问题,我不确定你的意思(你不知道你将在一个文件中有多少行数据?或者?)
#4
You'll want to check your compiler manual for the specific random number generator function, but chances are it generates random numbers between 0 and 1. This is easy to handle - you just scale the interval to be the proper width, then shift it to match the proper starting point: i.e. to map r
in [0, 1]
to s
in [a, b]
, use s = r*(b-a) + a
, where r
is the value you got from your random number generator and s
is a random value in the range you want.
您需要检查编译器手册中的特定随机数生成器函数,但有可能生成0到1之间的随机数。这很容易处理 - 您只需将间隔缩放为适当的宽度,然后将其移至匹配正确的起点:即将[0,1]中的r映射到[a,b]中的s,使用s = r *(ba)+ a,其中r是从随机数生成器得到的值和s是您想要的范围内的随机值。
Idigas's answer covers your second question well - read in data using formatted input, then use them as you would any other variable.
Idigas的答案很好地涵盖了你的第二个问题 - 使用格式化输入读取数据,然后像使用任何其他变量一样使用它们。
For your third question, you will need to define how many lines there are in the text file only if you want to do something with all of them - if you're looking at reading the line, processing it, then moving on, you can get by without knowing the number of lines ahead of time. However, if you are looking to store all the values in the file (e.g. having arrays of temperature, humidity, and pressure so you can compute vapor pressure statistics), you'll need to set up storage somehow. Typically in FORTRAN 77, this is done by pre-allocating an array of a size larger than you think you'll need, but this can quickly become problematic. Is there any chance of switching to Fortran 90? The updated version has much better facilities for dealing with standardized dynamic memory allocation, not to mention many other advantages. I would strongly recommend using F90 if at all possible - you will make your life much easier.
对于你的第三个问题,你需要定义文本文件中有多少行,只要你想对所有这些行做一些事情 - 如果你正在阅读这条线,处理它,然后继续,你可以在不知道提前行数的情况下过关。但是,如果您要将所有值存储在文件中(例如,具有温度,湿度和压力的数组,以便您可以计算蒸汽压力统计数据),则需要以某种方式设置存储。通常在FORTRAN 77中,这是通过预先分配大于您认为需要的大小的数组来完成的,但这很快就会成为问题。有没有机会切换到Fortran 90?更新版本具有更好的处理标准化动态内存分配的工具,更不用说许多其他优点。如果可能的话,我强烈建议使用F90 - 你会让生活更轻松。
Another option, depending on the type of processing you're doing, would be to investigate algorithms that use only single passes through data, so you won't need to store everything to compute things like means and standard deviations, for example.
另一种选择,取决于您正在进行的处理类型,将研究仅使用单次传递数据的算法,因此您不需要存储所有内容来计算诸如均值和标准偏差等内容。