671 Bytes, 下载次数: 9
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
fcode 发表于 2014-3-24 12:42
1.随机数需要种子。建议你加上
2.再随机取一个数X判断,如果X小于EXP(M2-M)这句话里的 X 是随机数,还是 ...
chuxf 发表于 2014-3-24 13:00
我写了个版本,看是否是你需要的。
[mw_shl_code=fortran,true]Program www_fcode_cn
欢迎光临 Fortran Coder (http://bbs.fcode.cn/) | Powered by Discuz! X3.2 |