title: Monte Carlo using Fortran
body: <p>I am attempting to do a Monte Carlo simulation using RANDOM_NUMBER. I am using gFortran. I want to perform the following:</p>
  
  <ol>
  <li>Calculate monteNum (fixed number) and generate a random number, monteTest.</li>
  <li>If monteNum >= monteTest, then generate another random number randPos which is used to select a row from an array.</li>
  <li>Otherwise, generate a new monteTest until step 2 is satisfied.</li>
  </ol>
  
  <p>Firstly, I tried to use a DO loop.</p>
  
  <pre><code>CALL RANDOM_SEED()
  monteNum = (count_up + count_dn)/(nReal**2) ! This is just a number in [0,1].
  DO i = 1, 100
    CALL RANDOM_NUMBER (monteTest) 
    ! monteTest is a randomly generated number used in Monte Carlo simulation
     IF (monteNum &gt;= monteTest) THEN
      CALL RANDOM_NUMBER (randPos)   
       ! randPos will be used to select one flippable position randomly
      Vpos = INT(randPos*count)   
       ! position of the chosen vertex; count is the length of fList
      flipVertex(1,:) = fList(Vpos,:)
    ELSE
      i = i+1
    END IF
  END DO
  </code></pre>
  
  <p>An error arises from the ELSE statement. Since it is not known that the IF statement will produce TRUE in 100 loops, I thought a DO WHILE was a better choice.</p>
  
  <pre><code>monteTest = 0.5 ! Setting the initial value. But ideally it should be random
  DO WHILE (monteNum &lt; monteTest)
    CALL RANDOM_NUMBER (monteTest) 
    CALL RANDOM_NUMBER (randPos)   
    Vpos = INT(randPos*count)   
    flipVertex(1,:) = fList(Vpos,:)
  END DO 
  </code></pre>
  
  <p>But it didn't work either. 
  The problem is that <code>randPos</code> is always zero for initial <code>monteTest = 0.2</code> and <code>randPos = 5.35517931E-03</code> for initial <code>monteTest = 0.5</code>. Here, the correct value of <code>monteNum</code> is 0.22222.
  I was expecting outputs to change every time I run it, but I am getting the same output every time. Why would that be? Am I using the <code>RANDOM_NUMBER</code> in a wrong way?
  Any help would be appreciated!</p>