summaryrefslogtreecommitdiff
path: root/Utilib/src/RANDF.f
blob: 9b9e6de81e48d43fa2fb699083cfab895e1d486f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
*DECK RANDF
      SUBROUTINE RANDF(ISEED,IFIRST,RAND)
*
*-----------------------------------------------------------------------
*
*Purpose:
* This subroutine returns a pseudo-random number for each invocation.
* It is a FORTRAN 77 adaptation of the "Integer Version 2" minimal
* standard number generator whose Pascal code appears in reference.
*
*Copyright:
* Copyright (C) 2008 Ecole Polytechnique de Montreal
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version
*
*Author(s): R. Chambon
*
*Parameters: input
* ISEED   the seed for the generation of random numbers.
* IFIRST  set to 1 to indicate that the seed is being generated.
*                 
*Parameters: ouput
* IFIRST  set to 1 to indicate that the seed is being generated.
* RAND    random number between 0 and 1.
*
*Reference:                     
* Park, Steven K. and Miller, Keith W., "Random Number Generators:
* Good Ones are Hard to Find", Communications of the ACM, October 1988.
*                          
*-----------------------------------------------------------------------
*
      IMPLICIT NONE
*----
*  SUBROUTINE ARGUMENTS
*----
      INTEGER ISEED,IFIRST
      REAL    RAND
      DOUBLE PRECISION D1,D2
*----
*  LOCAL VARIABLES
*----
      INTEGER  MPLIER,MODLUS,MOBYMP,MOMDMP
      PARAMETER (MPLIER=16807,MODLUS=2147483647,MOBYMP=127773,
     +           MOMDMP=2836)
      INTEGER HVLUE, LVLUE, TESTV, NEXTN
*
      IF (IFIRST .EQ. 0) IFIRST = 1
      NEXTN = ISEED
*
      HVLUE = NEXTN / MOBYMP
      LVLUE = MOD(NEXTN, MOBYMP)
      TESTV = MPLIER*LVLUE - MOMDMP*HVLUE
      IF (TESTV .GT. 0) THEN
        NEXTN = TESTV
      ELSE
        NEXTN = TESTV + MODLUS
      ENDIF
      D1=DBLE(NEXTN)
      D2=DBLE(MODLUS)
      RAND=REAL(D1/D2)
      ISEED= NEXTN
      RETURN
      END