[Fortran] 纯文本查看 复制代码
program main
implicit none
integer :: idum
real :: random_number
! 初始化随机数种子,这里可自行设置合适的整数值
idum = 1234
! 调用ran2函数获取随机数
random_number = ran2(idum)
print *, "生成的随机数为:", random_number
end program main
FUNCTION ran2(idum)
INTEGER idum, IM1, IM2, IMM1, IA1, IA2, IQ1, IQ2, IR1, IR2, NTAB, NDIV
REAL ran2, AM, EPS, RNMX
PARAMETER (IM1=2147483563,IM2=2147483399,AM=1./IM1, IMM1=IM1-1,IA1=40014,IA2=40692,IQ1=53668,IQ2=52774,IR1=12211, IR2=3791, NTAB=32,NDIV=1+IMM1/NTAB,EPS=1.2e-7,RNMX=1.-EPS)
INTEGER idum2, j, k, iv(NTAB), iy
SAVE iv, iy, idum2
DATA idum2/123456789/, iv/NTAB*0/,iy/0/
if (idum.le.0) then
idum=max(-idum,1)
idum2=idum
do j=NTAB+8,1,-1
k=idum/IQ1
idum=IA1*(idum-k*IQ1)-k*IR1
if (idum.lt.0) idum=idum+IM1
if (j.le.NTAB) iv(j)=idum
enddo
iy=iv(1)
endif
k=idum/IQ1
idum=IA1*(idum-k*IQ1)-k*IR1
if (idum.lt.0 ) idum=idum+IM1
k=idum2/IQ2
idum2=IA2*(idum2-k*IQ2)-k*IR2
if (idum2.lt.0) idum2=idum2+IM2
j=1+iy/NDIV
iy=iv(j)-idum2
iv(j)=idum
if (iy.lt.1) iy=iy+IMM1
ran2=min(AM*iy, RNMX)
return
END