public inbox for gcc-help@gcc.gnu.org
 help / color / mirror / Atom feed
* gfortran rand() not working properly?
@ 2020-07-29  6:47 tlake
  2020-07-29  8:58 ` Stefan Ring
  0 siblings, 1 reply; 2+ messages in thread
From: tlake @ 2020-07-29  6:47 UTC (permalink / raw)
  To: gcc-help

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


^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: gfortran rand() not working properly?
  2020-07-29  6:47 gfortran rand() not working properly? tlake
@ 2020-07-29  8:58 ` Stefan Ring
  0 siblings, 0 replies; 2+ messages in thread
From: Stefan Ring @ 2020-07-29  8:58 UTC (permalink / raw)
  To: tlake; +Cc: gcc-help

On Wed, Jul 29, 2020 at 8:48 AM <tlake@twcny.rr.com> wrote:
>
> 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

I don’t know Fortran, but it looks like this variable declaration is
conflicting with the invocation of the rand function.

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2020-07-29  8:58 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-07-29  6:47 gfortran rand() not working properly? tlake
2020-07-29  8:58 ` Stefan Ring

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).