gfortran rand() not working properly?

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



In the program below, the variable x is always a tiny number close to 0. It
never gets to be more than about 1e-314. Variable y does get the whole range
of rand() (0 - .9999999)

This leads to all estimates of PI being 4.00000.

If I add another variable t by uncommenting the line t=rand(), then t is
incorrect but both x and y get the full range of rand() and the program
works OK.

I'm using the 64-bit version of gfortran with MSYS2-64 on Win 10.

 

 

 

! Program to compute Pi using monte carlo methods

 

       program monte_pi

       implicit none

 

       integer niter,i,j

       integer seed

       real*4 count

       real *8 x,y,pi(100),z,t

       real*8   rand

 

! initialize random numbers

       seed = 35791246

       call srand (seed)

       do j= 1,100

          niter = niter+100

          count =0

          do i=1,niter

!             t = rand()

             x=rand()

             y=rand()

             z= x*x +y*y

             if (z .le. 1) count =count+1

          end do

          pi(j)= count/niter*4.

          write(*,10) niter,pi(j)

10        format('Number of trials is: 'i5,'  estimate of pi is:',f8.5)

       end do

       end




[Index of Archives]     [Linux C Programming]     [Linux Kernel]     [eCos]     [Fedora Development]     [Fedora Announce]     [Autoconf]     [The DWARVES Debugging Tools]     [Yosemite Campsites]     [Yosemite News]     [Linux GCC]

  Powered by Linux