Friday, January 11, 2008

RAND、RANDOM_NUMBER(ZZ)

from http://140.136.191.181/html/frank/document/fortran/intrinsic_function/random_number.htm

RAND、RANDOM_NUMBER(取亂數)


※請注意,Fortran 90 有新語法

o2000 中,rand的說明文件,適用Fortran 77 與 90:
【請注意:ran 為 real 型態,rand 為 double 型態】
RAND(3F)                                                              RAND(3F)

NAME
rand, irand, srand - random number generator

SYNOPSIS
integer iseed, i, irand
double precision x, rand

call srand(iseed)

i = irand( )

x = rand( )

DESCRIPTION
Irand generates successive pseudo-random integers in the range from 0 to
2**15-1. rand generates pseudo-random numbers distributed in [0, 1.0].
Srand uses its integer argument to re-initialize the seed for successive
invocations of irand and rand.

SEE ALSO
rand(3C).

Page 1

RAN(3I)                                                Last changed: 1-6-98

NAME
_ranf, RANF, RANGET, RANSET - Computes pseudo-random numbers

SYNOPSIS
C/C++:

#include

double _ranf(void);

Fortran:

RANF()

RANGET ([I=]i)

RANSET ([K=]k)

Fortran on UNICOS/mk systems only:

RANSET ([K=]k [, [J=]j])

IMPLEMENTATION
C/C++: UNICOS and UNICOS/mk systems

Fortran: UNICOS, UNICOS/mk, and IRIX systems

STANDARDS
C/C++: Cray Research extension

Fortran: CF90 compiler extension to Fortran 90

DESCRIPTION
The _ranf and RAND functions return a pseudo-random floating-point
number in the range of 0.0 < x < 1.0.

The RANGET intrinsic procedure returns a seed from the random number
seed table.

The RANSET function establishes a seed in the random number seed
table.

C/C++ NOTES
Because _ranf is an intrinsic function, no externally visible library
function is available for it.

FORTRAN NOTES
RANF obtains the first or next in a series of pseudo-random numbers.
Parenthesis are required, that is: var = RANF(). If an argument is
supplied, it is ignored.

The RANGET intrinsic function obtains a seed. It can be called as a
function or a subroutine; it is recommended that this routine be used
as a function. RANGET has an optional integer argument. If present,
the argument is set to the seed. The argument for RANGET is as
follows:

i An integer of default kind (KIND=8). If present, RANGET returns
the seed in i.

On UNICOS and UNICOS/mk systems, the RANSET intrinsic function
establishes a seed by using the lower 48 bits of the argument. The
result type is typeless. If no argument or a zero argument is
supplied, the seed is reset to an initial default value. When the
seed of the random number generator is reset, RANSET does not store
the supplied argument as the first value in the buffer of the random
number seeds. If an argument is supplied, the lower 48 bits are used
as the random-number seed. The rightmost bit is always set to 1.

The RANSET arguments are as follows:

k An optional integer, real, or Boolean argument of default kind
(KIND=8) The range of argument k is |k| < inf, where inf is as
follows:
2450
* On UNICOS systems, infinity is approximately 10 .
308
* On UNICOS/mk and IRIX systems, infinity is approximately 10 .

j An optional integer argument that, if specified, is used as a
count for skipping the first section of sequential random
numbers. You can use this to create a complete sequence of
random numbers while running on many PEs by breaking up the
sequence into subsequences and using RANSET() to get each
subsequence started in the correct location.

The names of these intrinsics cannot be passed as arguments.

THE RANF ALGORITHM
In Fortran on CRAY C90 systems, the random number generation algorithm
uses the following two equations:

* S(n+1) = M1*S(n) mod 2**48

* S(n+64) = M64*S(n) mod 2**48

Each S(i) is the ith seed.

The first equation is used to generate the first 128 random numbers
and store them in a table if a call to RANSET() was done. Otherwise,
the table contains the first 128 random numbers from the default seed.
The second equation is used because it vectorizes.

The default seed is S(0) = 1274321477413155 (octal).

The operations M1*S(n) and M64*S(n) are done as integer
multiplications in such a way to preserve the lower 48 bits. It is
the lower 48 bits that make the next random number seed and are used
in the return value.

The return value (random number) is the newly generated seed with an
exponent of 40000 (octal) added. This is normalized before exit.

The multiplier M1 is 1207264271730565 (octal) and is related to M64 by
the following expression:
M64 = lower 48 bits of M1**64 = 7027423346125401 (octal).

For example, the following Fortran program, when compiled with the
f90 -i 64 option on a CRAY C90 system, is the equivalent of the RANF
function:

cc
c THIS IS A FORTRAN VERSION OF RANF() FUNCTION
cc
REAL FUNCTION RANF()
REAL NORM
INTEGER MHI,MLO,EXPO,SEED,SEEDHI,SEEDLO
DATA SEED/1274321477413155B/
SAVE SEED

MHI = 12072642B
MLO = 71730565B

EXPO = SHIFTL(40000B,48)

SEEDHI = SHIFTR(AND(SHIFTL(77777777B,24),SEED),24)
SEEDLO = AND(77777777B,SEED)

SEED = AND(7777777777777777B,SEEDLO*MLO+
1 SHIFTL(SEEDLO*MHI+SEEDHI*MLO,24))

RANF=NORM(OR(EXPO,SEED),0.0)
RETURN
END
cc
c THIS IS HERE TO NORMALIZE THE FLOATING POINT RESULT
cc
REAL FUNCTION NORM(X,Y)
REAL X,Y
NORM = X+Y
RETURN
END

On IRIX systems, RANF uses a 64-bit linear congruential generator with
a default seed of 1274321477413155 (octal).

THE RANF REPEAT PERIOD
In Fortran, the period of RANF() is 2**46. If you need to insure that
two random number ranges do not overlap, you can determine this
empirically by generating the two sets of numbers and comparing them
against one another, and also against themselves, for an overlap. It
should be noted, however, that when using RANSET to set the random
number seed, the algorithm used always rounds up even-numbered seeds
to the nearest odd-numbered seed (that is, the right most bit is
always set to one). Some adjacent pairs of seeds will generate
exactly the same set of random numbers. For example, seeds 4 and 5
will generate the same set of random numbers.

RANF AND MULTITASKING
In Fortran, the random number generator uses static memory storage for
the random number seed table, so the RANF, RANSET, and RANGET
functions must be protected (locked) when called from a multitasked
program.

RANF generates a set of random numbers such that each random number
depends on the previous random number for its value. Thus, depending
on the order in which the tasks calling RANF execute, a different set
of random numbers will be returned to each task. It cannot be
guaranteed that each task will get a distinct and reproducible set of
random number values.

RETURN VALUES
_ranf and RANF return a 64-bit floating-point number in the range
0.0 < x < 1.0.

RANGET returns a 64-bit integer result.

RANSET returns a 64-bit typeless result.

EXAMPLES
The following examples are written in Fortran:

DO 10 I=1,10
10 RANDOM(I)=RANF()

CALL RANGET(iseed1)
C or
iseed=RANGET(ivalue)

CALL RANSET(ivalue)
C or
dummy=RANSET(ivalue)

SEE ALSO
RANDOM_NUMBER(3I), RANDOM_SEED(3I)

rand(3C) in the UNICOS System Libraries Reference Manual, publication
SR-2080

A complete list of C/C++ intrinsic functions available on Cray
Research systems is in the Cray C/C++ Reference Manual, publication SR
-2179.

Intrinsic Procedures Reference Manual, publication SR-2138, for the
printed version of this man page.

提醒:

  1. 範例程式中,rand部分,變數 x , rand 型態需宣告為 double precision,否則會傳回 0.0。

  2. 若要使用real型態,請用 ran( )。

  3. iseed 若未提供或每次一樣,每次執行所得亂數相同,可利用如時間函數

    • o2000下使用 f77:secnds(1.)可傳回系統秒數

    • MS-Fortran:利用GETTIM(ihr,imin,isec,i100th)函數,傳回十、分、秒、百分之一秒


關鍵字:pseudorandom numbers(偽隨機數)

Fortran 90 透過 random_seed 做初始設定,並透過 random_number 傳回介於 0 到 1 的亂數值。

若需要的是介於 A 到 B 的亂數值,可利用 A+ (B-A) * RandomNumber

或 M + INT (K * RandomNumber) ,可得到 M 到 M+K-1 的隨機整數

o2000 中,random_number的說明文件,適用Fortran 90:
RANDOM_NUMBER(3I)                                      Last changed: 1-6-98

NAME
RANDOM_NUMBER - Returns pseudorandom numbers

SYNOPSIS
CALL RANDOM_NUMBER ([HARVEST=]harvest)

IMPLEMENTATION
UNICOS, UNICOS/mk, and IRIX systems

STANDARDS
Fortran 90

DESCRIPTION
The RANDOM_NUMBER intrinsic subroutine returns one pseudorandom number
or an array of pseudorandom numbers from the uniform distribution over
the range 0 <= x < 1. It accepts the following argument:

harvest Must be of type real. It is an output argument. It may be
a scalar or an array variable. It is set to contain
pseudorandom numbers from the uniform distribution in the
interval 0 <= x < 1.
harvest Must be of type real. It is an output argument. It may be
a scalar or an array variable. It is set to contain
pseudorandom numbers from the uniform distribution in the
interval 0 <= x < 1.

On UNICOS and UNICOS/mk systems, harvest is 64 bits. On
IRIX systems, harvest is 32 bits.

The name of this intrinsic cannot be passed as an argument.

EXAMPLES
REAL X, Y(10,10)
! Initialize X with a pseudorandom number
CALL RANDOM_NUMBER(HARVEST=X)
CALL RANDOM_NUMBER(Y)
! X and Y contain uniformly distributed random numbers

SEE ALSO
RANDOM_SEED(3I), RANF(3I)

Intrinsic Procedures Reference Manual, publication SR-2138, for the
printed version of this man page.

 

o2000 中,random_seed的說明文件,適用Fortran 90:
RANDOM_SEED(3I)                                        Last changed: 1-6-98

NAME
RANDOM_SEED - Restarts or queries the pseudorandom number generator

SYNOPSIS
CALL RANDOM_SEED ([[SIZE=]size] [,[PUT=]put] [,[GET=]get])

IMPLEMENTATION
UNICOS, UNICOS/mk, and IRIX systems

STANDARDS
Fortran 90

DESCRIPTION
The RANDOM_SEED intrinsic subroutine restarts or queries the
pseudorandom number generator used by RANDOM_NUMBER. It accepts the
following arguments (note that there must be exactly one argument or
no arguments present):

size Must be scalar and of type integer. It is an output argument.
It is set to the number N of integers that the processor uses
to hold the value of the seed.

put Must be an integer array of rank one and size >= N. It is an
input argument. It is used to set the seed value.

get Must be an integer array of rank one and size >= N. It is an
output argument. It is set to the current value of the seed.

The name of this intrinsic cannot be passed as an argument.

EXAMPLES
CALL RANDOM_SEED ! initization of seed
CALL RANDOM_SEED(SIZE = K) ! Sets K = N
CALL RANDOM_SEED(PUT = SEEDARRAY(1:K)) ! Set user seed
CALL RANDOM_SEED(GET = OLDSEED(1:K)) ! Get current seed

SEE ALSO
RANDOM_NUMBER(3I)

Intrinsic Procedures Reference Manual, publication SR-2138, for the
printed version of this man page.

 

o2000 中,ranf的說明文件:
RAN(3I)                                                Last changed: 1-6-98

NAME
_ranf, RANF, RANGET, RANSET - Computes pseudo-random numbers

SYNOPSIS
C/C++:

#include

double _ranf(void);

Fortran:

RANF()

RANGET ([I=]i)

RANSET ([K=]k)

Fortran on UNICOS/mk systems only:

RANSET ([K=]k [, [J=]j])
RAN(3I) Last changed: 1-6-98

NAME
_ranf, RANF, RANGET, RANSET - Computes pseudo-random numbers

SYNOPSIS
C/C++:

#include

double _ranf(void);

Fortran:

RANF()

RANGET ([I=]i)

RANSET ([K=]k)

Fortran on UNICOS/mk systems only:

RANSET ([K=]k [, [J=]j])
RAN(3I) Last changed: 1-6-98

NAME
_ranf, RANF, RANGET, RANSET - Computes pseudo-random numbers

SYNOPSIS
C/C++:

#include

double _ranf(void);

Fortran:

RANF()

RANGET ([I=]i)

RANSET ([K=]k)

Fortran on UNICOS/mk systems only:

RANSET ([K=]k [, [J=]j])
RAN(3I) Last changed: 1-6-98

NAME
_ranf, RANF, RANGET, RANSET - Computes pseudo-random numbers

SYNOPSIS
C/C++:

#include

double _ranf(void);

Fortran:

RANF()

RANGET ([I=]i)

RANSET ([K=]k)

Fortran on UNICOS/mk systems only:

RANSET ([K=]k [, [J=]j])

FORTRAN NOTES
RANF obtains the first or next in a series of pseudo-random numbers.
Parenthesis are required, that is: var = RANF(). If an argument is
supplied, it is ignored.

The RANGET intrinsic function obtains a seed. It can be called as a
function or a subroutine; it is recommended that this routine be used
as a function. RANGET has an optional integer argument. If present,
the argument is set to the seed. The argument for RANGET is as
follows:

i An integer of default kind (KIND=8). If present, RANGET returns
the seed in i.

On UNICOS and UNICOS/mk systems, the RANSET intrinsic function
establishes a seed by using the lower 48 bits of the argument. The
result type is typeless. If no argument or a zero argument is
supplied, the seed is reset to an initial default value. When the
seed of the random number generator is reset, RANSET does not store
the supplied argument as the first value in the buffer of the random
number seeds. If an argument is supplied, the lower 48 bits are used
as the random-number seed. The rightmost bit is always set to 1.

The RANSET arguments are as follows:

k An optional integer, real, or Boolean argument of default kind
(KIND=8) The range of argument k is |k| < inf, where inf is as
follows:
2450
* On UNICOS systems, infinity is approximately 10 .
308
* On UNICOS/mk and IRIX systems, infinity is approximately 10 .

j An optional integer argument that, if specified, is used as a
count for skipping the first section of sequential random
numbers. You can use this to create a complete sequence of
random numbers while running on many PEs by breaking up the
sequence into subsequences and using RANSET() to get each
subsequence started in the correct location.

The names of these intrinsics cannot be passed as arguments.

THE RANF ALGORITHM
In Fortran on CRAY C90 systems, the random number generation algorithm
uses the following two equations:

* S(n+1) = M1*S(n) mod 2**48

* S(n+64) = M64*S(n) mod 2**48

Each S(i) is the ith seed.

The first equation is used to generate the first 128 random numbers
and store them in a table if a call to RANSET() was done. Otherwise,
the table contains the first 128 random numbers from the default seed.
The second equation is used because it vectorizes.

The default seed is S(0) = 1274321477413155 (octal).

The operations M1*S(n) and M64*S(n) are done as integer
multiplications in such a way to preserve the lower 48 bits. It is
the lower 48 bits that make the next random number seed and are used
in the return value.

The return value (random number) is the newly generated seed with an
exponent of 40000 (octal) added. This is normalized before exit.

The multiplier M1 is 1207264271730565 (octal) and is related to M64 by
the following expression:
M64 = lower 48 bits of M1**64 = 7027423346125401 (octal).

For example, the following Fortran program, when compiled with the
f90 -i 64 option on a CRAY C90 system, is the equivalent of the RANF
function:

cc
c THIS IS A FORTRAN VERSION OF RANF() FUNCTION
cc
REAL FUNCTION RANF()
REAL NORM
INTEGER MHI,MLO,EXPO,SEED,SEEDHI,SEEDLO
DATA SEED/1274321477413155B/
SAVE SEED

MHI = 12072642B
MLO = 71730565B

EXPO = SHIFTL(40000B,48)

SEEDHI = SHIFTR(AND(SHIFTL(77777777B,24),SEED),24)
SEEDLO = AND(77777777B,SEED)

SEED = AND(7777777777777777B,SEEDLO*MLO+
1 SHIFTL(SEEDLO*MHI+SEEDHI*MLO,24))

RANF=NORM(OR(EXPO,SEED),0.0)
RETURN
END
cc
c THIS IS HERE TO NORMALIZE THE FLOATING POINT RESULT
cc
REAL FUNCTION NORM(X,Y)
REAL X,Y
NORM = X+Y
RETURN
END

On IRIX systems, RANF uses a 64-bit linear congruential generator with
a default seed of 1274321477413155 (octal).

THE RANF REPEAT PERIOD
In Fortran, the period of RANF() is 2**46. If you need to insure that
two random number ranges do not overlap, you can determine this
empirically by generating the two sets of numbers and comparing them
against one another, and also against themselves, for an overlap. It
should be noted, however, that when using RANSET to set the random
number seed, the algorithm used always rounds up even-numbered seeds
to the nearest odd-numbered seed (that is, the right most bit is
always set to one). Some adjacent pairs of seeds will generate
exactly the same set of random numbers. For example, seeds 4 and 5
will generate the same set of random numbers.

RANF AND MULTITASKING
In Fortran, the random number generator uses static memory storage for
the random number seed table, so the RANF, RANSET, and RANGET
functions must be protected (locked) when called from a multitasked
program.

RANF generates a set of random numbers such that each random number
depends on the previous random number for its value. Thus, depending
on the order in which the tasks calling RANF execute, a different set
of random numbers will be returned to each task. It cannot be
guaranteed that each task will get a distinct and reproducible set of
random number values.

RETURN VALUES
_ranf and RANF return a 64-bit floating-point number in the range
0.0 < x < 1.0.

RANGET returns a 64-bit integer result.

RANSET returns a 64-bit typeless result.

EXAMPLES
The following examples are written in Fortran:

DO 10 I=1,10
10 RANDOM(I)=RANF()

CALL RANGET(iseed1)
C or
iseed=RANGET(ivalue)

CALL RANSET(ivalue)
C or
dummy=RANSET(ivalue)

SEE ALSO
RANDOM_NUMBER(3I), RANDOM_SEED(3I)

rand(3C) in the UNICOS System Libraries Reference Manual, publication
SR-2080

A complete list of C/C++ intrinsic functions available on Cray
Research systems is in the Cray C/C++ Reference Manual, publication SR
-2179.

Intrinsic Procedures Reference Manual, publication SR-2138, for the
printed version of this man page.

Sunday, January 06, 2008

一剪梅

一剪梅
李清照
红藕香残玉蕇秋,
轻解罗裳,独上兰舟。
云中谁寄锦书来?
雁字回时,月满西楼。

花自飘零水自流,
一种相思,两处闲愁。
此情无处可消除,
才下眉头,却上心头。

Friday, January 04, 2008

在Linux下搭建Fortran开发环境 (ZZ)

在Linux下搭建Fortran开发环境

何勃亮

Fortran是最早的计算机高级语言,发展至今已经有40年左右的时间了,其中经历了Fortran 66、Fortran 77、Fortran 90、Fortran95已经目前还在草案过程中的Fortran 200x。而在科学计算领域,Fortran仍具有十分强大的生命力。在大型科学计算中还有HPF(High Performance Fortran)等。