Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Simple Fortran Program With Random Numbers

  1. Mar 1, 2010 #1
    I have been busy trying to generate, using FORTRAN's random number generator, random x and y co-ordinates which follow a simple pattern such as x2-100x, but for some reason the FORTRAN compiler prints nothing.

    Anybody want to help me with this problem?

    Code (Text):
        program quadraticdistribution
        implicit none
        integer i
        integer x,y
        real l
        integer seed/-23434567/
    c     ************************************
    c     intialize the random number
          l=rand(seed)

    c     intialize the x and y values
          x=0
          y=0
           
    30      do i=1,200
                x=int(100*rand(0))
                y=int(100*rand(0))
            if (x.ge.(y*y-100*y)) goto 30
                Write(*,*) x,y
            enddo
               
               stop
               end


           
       
     
  2. jcsd
  3. Mar 1, 2010 #2

    minger

    User Avatar
    Science Advisor

    What compiler are you using? A lot of random stuff I've seen is compiler-specific. As a G95 user, I'm not familiar with your approach.
     
  4. Mar 1, 2010 #3

    Mark44

    Staff: Mentor

    Since you're not getting any output, it's probably the case that your if statement is preventing the following write statement from executing. I added a write statement right after x and y get set, so you can at least get some output.

    The main problem, I believe, is your goto in the middle of your loop. I changed the logic in your if statement so that if x < y^2 - 100y, the code prints the values of x and y. If x >= y^2 - 100y, the code doesn't print anything.
    Code (Text):

        program quadraticdistribution
        implicit none
        integer i
        integer x,y
        real l
        integer seed/-23434567/
    c     ************************************
    c     intialize the random number
          l=rand(seed)

    c     intialize the x and y values
          x=0
          y=0
           
          do i=1,200
             x=int(100*rand(0))
             y=int(100*rand(0))
             write(*, *) x, y   ; for debugging purposes
             if (x .lt. (y*y-100*y))
                Write(*,*) x,y
             endif
            enddo
               
               stop
               end
     
     
  5. Mar 3, 2010 #4
    If y lies between 0 and 100, y^2-100*y is always negative, and the condition x>y^2-100*y is always true.
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook




Similar Discussions: Simple Fortran Program With Random Numbers
Loading...