在 Fortran 77 中生成给定范围内的随机数

发布于 2024-07-20 06:10:53 字数 259 浏览 5 评论 0原文

我是一个初学者,尝试使用 fortran 77 做一些工程实验。我使用 Force 2.0 编译器和编辑器。 我有以下疑问:

  1. 如何生成指定范围内的随机数,例如,如果我需要生成 3.0 到 10.0 之间的单个随机数,我该怎么做?
  2. 如何使用文本文件中的数据在程序的计算中调用。 例如,我有温度、压力和湿度值(一天中每小时的值,因此每个文本文件中总共有 24 个值)。
  3. 我还需要在程序中定义文本文件中有多少个值吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

谎言 2024-07-27 06:10:53

第二个问题:

例如,如果您的文件看起来像:

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

这个简单的程序将读取它:(

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

缩进有点搞砸了,但我不知道如何在这个奇怪的环境中正确设置它)

我的建议:阅读关于数据类型(INTEGER、REAL、CHARACTER)、数组(DIMENSION)、输入/输出(READ、WRITE、OPEN、CLOSE、REWIND)和循环(DO、FOR),您很快就会做有用的事情。

我从来没有对随机数做过任何事情,所以我无法帮助你,但我认为 fortran 中有一些内在的函数可以做到这一点。 我去查一下,明天报告。 至于第三个问题,我不确定你的意思(你不知道一个文件中有多少行数据?或?)

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 ?)

夏末的微笑 2024-07-27 06:10:53

您需要检查编译器手册以了解特定的随机数生成器函数,但它很可能会生成 0 到 1 之间的随机数。这很容易处理 - 您只需将间隔缩放到适当的宽度,然后将其转换为匹配正确的起点:即将[0, 1]中的r映射到[a, b]中的s code>,使用 s = r*(ba) + a,其中 r 是您从随机数生成器获得的值,s 是您想要的范围内的随机值。

Idigas 的答案涵盖了您的第二个问题很好 - 使用格式化输入读取数据,然后像使用任何其他变量一样使用它们。

对于第三个问题,只有当您想对所有行执行某些操作时,您才需要定义文本文件中有多少行 - 如果您正在考虑读取该行,处理它,然后继续,您可以在不知道提前行数的情况下顺利通过。 但是,如果您希望存储文件中的所有值(例如,具有温度、湿度和压力数组,以便可以计算蒸气压统计数据),则需要以某种方式设置存储。 通常在 FORTRAN 77 中,这是通过预先分配一个大小大于您认为需要的数组来完成的,但这很快就会出现问题。 有机会改用 Fortran 90 吗? 更新的版本在处理标准化动态内存分配方面拥有更好的设施,更不用说许多其他优点了。 如果可能的话,我强烈推荐使用F90 - 你会让你的生活变得更轻松。

另一种选择是研究仅使用单遍数据的算法,具体取决于您正在执行的处理类型,因此您不需要存储所有内容来计算平均值和标准差等内容。

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.

Idigas's answer covers your second question well - read in data using formatted input, then use them as you would any other variable.

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.

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.

錯遇了你 2024-07-27 06:10:53

该子例程在 Fortran 77 中生成 0 到 ifin 之间的随机数
其中 i 是种子; 一些很大的数字,例如 746397923

    subroutine rnd001(xi,i,ifin)
    integer*4 i,ifin
    real*8 xi
    i=i*54891
    xi=i*2.328306e-10+0.5D00
    xi=xi*ifin
    return
    end

您可以修改以获取一定的范围。

This subroutine generate a random number in fortran 77 between 0 and ifin
where i is the seed; some great number such as 746397923

    subroutine rnd001(xi,i,ifin)
    integer*4 i,ifin
    real*8 xi
    i=i*54891
    xi=i*2.328306e-10+0.5D00
    xi=xi*ifin
    return
    end

You may modifies in order to take a certain range.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文