本帖最后由 chuxf 于 2014-3-24 13:04 编辑
我写了个版本,看是否是你需要的。
[Fortran] 纯文本查看 复制代码 Program www_fcode_cn
Implicit None
Integer , parameter :: DP = Kind(0.0D0)
Integer :: s( 100 )
integer :: i , k , l
Real( Kind = DP ) :: m1 , x , y , z , m2
open (365,file='data.dat')
call random_seed()
do i=1,size(s)
call random_number(x)
if (x.le.0.5) then
s(i)=1
else
s(i)=-1
end if
end do
m1 = CalcM( S )
Do k = 1 , 10000
call random_number(y)
l=1+aint(100*y)
s(l)=-s(l)
m2 = CalcM( S )
call random_number(z)
i=1+aint(100*z)
if (s(i) < exp(m2-m1)) s(l)=-s(l)
m1=m2
write(365,*) m1
end do
contains
Real( Kind = DP ) Function CalcM( s )
Integer :: s(:)
integer :: j , n
n = size( s )
CalcM = -1.0_DP * s(1) * s(n)
Do j = 1 , n - 1
CalcM = CalcM - s(j) * s(j+1)
end do
End Function CalcM
End Program www_fcode_cn |