PROGRAM RandomNumber ! ------------------------------------------------------------------------------ ! ! Copyright 2007 Matthew Norton ! ! ------------------------------------------------------------------------------ ! This program is free software; you can redistribute it and/or modify ! it under the terms of the GNU General Public License as published by ! the Free Software Foundation; either version 2 of the License, or ! (at your option) any later version. ! ! This program is distributed in the hope that it will be useful, ! but WITHOUT ANY WARRANTY; without even the implied warranty of ! MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the ! GNU General Public License for more details. ! ! You should have received a copy of the GNU General Public License ! along with this program; if not, write to the Free Software ! Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA ! ! NOTE: This is in FORTRAN 90 and you will need a FORTRAN 90 complier to run ! this code ! ------------------------------------------------------------------------------ ! ! This program calculates the derivative of cos(x) when it is evaluated at x = 1 ! This program uses the 3-point, 2 point forward, 2 point backward, and the ! 5-point approximation methods. This program also calculates the error ! associated with each method. ! ! ------------------------------------------------------------------------------ IMPLICIT NONE REAL :: GRandom, BRandom, GRand(10000), BRand(10000) INTEGER :: I = 0, L = 0, ErrorStat = 0, NumofNum = 0, LL = 0 OPEN (UNIT = 20, FILE = "Random.txt", Status = "UNKNOWN", IOSTAT = ErrorStat) IF (ErrorStat /= 0) THEN STOP "*** Error in Opening File***" END IF L = 256 LL = 256 NumofNum = 10000 DO I = 1, NumofNum GRandom = GRandomNum(L) BRandom = BRandomNum(LL) GRand(I) = GRandom BRand(I) = BRandom WRITE (20,*) I, GRandom, BRandom WRITE (*,*) I, GRandom, BRandom END DO PRINT *, "Good Average:", SUM(GRand) / 10000. PRINT *, "Evil Average:", SUM(BRand) / 10000. ! ------------------------------------------------------------------------------ CONTAINS ! ------------------------------------------------------------------------------ FUNCTION GRandomNum (L) INTEGER, INTENT (INOUT) :: L REAL :: GRandomNum L = MOD(1366 * L + 150889, 714025) GRandomNum = REAL(L) / REAL(714024) RETURN END FUNCTION ! ------------------------------------------------------------------------------ FUNCTION BRandomNum (L) INTEGER, INTENT (INOUT) :: L REAL :: BRandomNum L = MOD(137 * L + 187, 256) BRandomNum = REAL(L) / REAL(255) RETURN END FUNCTION END PROGRAM RandomNumber