蒲豐氏投針問題的模擬過程,隨機數(shù)發(fā)生器也是自編的,以供大家參考和提出建議。謝謝。(seed1和seed2最好選擇3和5,為了使投針次數(shù)達到1000000,CVF進行如下設置Project->settings->link->
output,將stack allocations reserve:設為1000000000) CODE: program getpi
implicit none
real,parameter::a=5,L=4,pi=3.14159
integer::n1,i,counter=0
real,allocatable::R1(:),R2(:)
real::theta,x,pi1
write(*,*) 'input the size of the array:'
read(*,*) n1
allocate(R1(n1))
allocate(R2(n1))
call random(n1,R1,R2)
do i=1,n1
x=a*(2*R1(i)-1)
theta=pi*R2(i)
if(abs(x)
end do
pi1=(1.0*n1)/(counter*1.0)*(2.0*L/a)
write(*,*) 'this is PI:'
write(*,*) pi
write(*,"('this is ratio of counter to total number',F10.6)") 1.0
&*counter/n1
stop
end program
subroutine random(n,R1,R2)
implicit none
integer n,seed1,seed2,i,little,m
real::R1(n),R2(n)
integer::temp1(n),temp2(n)
write(*,*) 'input seed1'
read(*,*) seed1
write(*,*) 'input seed2'
read(*,*) seed2
m=2**30
m=m*2-1
temp1(1)=397204094
temp2(1)=temp1(1)
R1(1)=(1.0*temp1(1))/(1.0*m)
R2(1)=(1.0*temp2(1))/(1.0*m)
little=0
if(R1(1)<0.5) little=little+1
do i=1,n-1
temp1(i+1)=mod(seed1*temp1(i),m)
R1(i+1)=(1.0*temp1(i+1))/(1.0*m)
temp2(i+1)=mod(seed2*temp2(i),m)
R2(i+1)=(1.0*temp2(i+1))/(1.0*m)
if(R1(i+1)<0.5) little=little+1 .
end do ;
write(*,*) 'ratio of number which is little than 0.5'
write(*,*) 1.0*little/n
return
end subroutine
[ Last edited by zyj8119 on 2010-11-12 at 16:09 ]