function random (is) c A random number generator called as a function by c random (iseed) or irandm (iseed) c The parameter is called by reference, not by value. c The routine(s) are callable directly as Fortran functions. c From C, I expect you have to pass a pointer to a 2-element vector c The first call gives a real uniform in 0 .. 1. c The second gives an integer uniform in 0 .. 2**31-1 c Both update iseed in exactly the same way. c iseed must be a 2-element integer vector. c The initial value of the second element may be anything. c c The period of the random sequence is 2**32 * (2**32-1) c The table mt(0:127) is defined by mt(i) = 69069 ** (128-i) c parameter (mask = 593970775) c = hex 23674657 parameter (scale = 2.0 ** -31) c dimension is(2), mt(0:127) c data mt( 0 )/ 902906369 / data mt( 1 )/ 2030498053 / data mt( 2 )/ -473499623 / data mt( 3 )/ 1640834941 / data mt( 4 )/ 723406961 / data mt( 5 )/ 1993558325 / data mt( 6 )/ -257162999 / data mt( 7 )/ -1627724755 / data mt( 8 )/ 913952737 / data mt( 9 )/ 278845029 / data mt( 10 )/ 1327502073 / data mt( 11 )/ -1261253155 / data mt( 12 )/ 981676113 / data mt( 13 )/ -1785280363 / data mt( 14 )/ 1700077033 / data mt( 15 )/ 366908557 / data mt( 16 )/ -1514479167 / data mt( 17 )/ -682799163 / data mt( 18 )/ 141955545 / data mt( 19 )/ -830150595 / data mt( 20 )/ 317871153 / data mt( 21 )/ 1542036469 / data mt( 22 )/ -946413879 / data mt( 23 )/ -1950779155 / data mt( 24 )/ 985397153 / data mt( 25 )/ 626515237 / data mt( 26 )/ 530871481 / data mt( 27 )/ 783087261 / data mt( 28 )/ -1512358895 / data mt( 29 )/ 1031357269 / data mt( 30 )/ -2007710807 / data mt( 31 )/ -1652747955 / data mt( 32 )/ -1867214463 / data mt( 33 )/ 928251525 / data mt( 34 )/ 1243003801 / data mt( 35 )/ -2132510467 / data mt( 36 )/ 1874683889 / data mt( 37 )/ -717013323 / data mt( 38 )/ 218254473 / data mt( 39 )/ -1628774995 / data mt( 40 )/ -2064896159 / data mt( 41 )/ 69678053 / data mt( 42 )/ 281568889 / data mt( 43 )/ -2104168611 / data mt( 44 )/ -165128239 / data mt( 45 )/ 1536495125 / data mt( 46 )/ -39650967 / data mt( 47 )/ 546594317 / data mt( 48 )/ -725987007 / data mt( 49 )/ 1392966981 / data mt( 50 )/ 1044706649 / data mt( 51 )/ 687331773 / data mt( 52 )/ -2051306575 / data mt( 53 )/ 1544302965 / data mt( 54 )/ -758494647 / data mt( 55 )/ -1243934099 / data mt( 56 )/ -75073759 / data mt( 57 )/ 293132965 / data mt( 58 )/ -1935153095 / data mt( 59 )/ 118929437 / data mt( 60 )/ 807830417 / data mt( 61 )/ -1416222507 / data mt( 62 )/ -1550074071 / data mt( 63 )/ -84903219 / data mt( 64 )/ 1355292929 / data mt( 65 )/ -380482555 / data mt( 66 )/ -1818444007 / data mt( 67 )/ -204797315 / data mt( 68 )/ 170442609 / data mt( 69 )/ -1636797387 / data mt( 70 )/ 868931593 / data mt( 71 )/ -623503571 / data mt( 72 )/ 1711722209 / data mt( 73 )/ 381210981 / data mt( 74 )/ -161547783 / data mt( 75 )/ -272740131 / data mt( 76 )/ -1450066095 / data mt( 77 )/ 2116588437 / data mt( 78 )/ 1100682473 / data mt( 79 )/ 358442893 / data mt( 80 )/ -1529216831 / data mt( 81 )/ 2116152005 / data mt( 82 )/ -776333095 / data mt( 83 )/ 1265240893 / data mt( 84 )/ -482278607 / data mt( 85 )/ 1067190005 / data mt( 86 )/ 333444553 / data mt( 87 )/ 86502381 / data mt( 88 )/ 753481377 / data mt( 89 )/ 39000101 / data mt( 90 )/ 1779014585 / data mt( 91 )/ 219658653 / data mt( 92 )/ -920253679 / data mt( 93 )/ 2029538901 / data mt( 94 )/ 1207761577 / data mt( 95 )/ -1515772851 / data mt( 96 )/ -236195711 / data mt( 97 )/ 442620293 / data mt( 98 )/ 423166617 / data mt( 99 )/ -1763648515 / data mt( 100 )/ -398436623 / data mt( 101 )/ -1749358155 / data mt( 102 )/ -538598519 / data mt( 103 )/ -652439379 / data mt( 104 )/ 430550625 / data mt( 105 )/ -1481396507 / data mt( 106 )/ 2093206905 / data mt( 107 )/ -1934691747 / data mt( 108 )/ -962631983 / data mt( 109 )/ 1454463253 / data mt( 110 )/ -1877118871 / data mt( 111 )/ -291917555 / data mt( 112 )/ -1711673279 / data mt( 113 )/ 201201733 / data mt( 114 )/ -474645415 / data mt( 115 )/ -96764739 / data mt( 116 )/ -1587365199 / data mt( 117 )/ 1945705589 / data mt( 118 )/ 1303896393 / data mt( 119 )/ 1744831853 / data mt( 120 )/ 381957665 / data mt( 121 )/ 2135332261 / data mt( 122 )/ -55996615 / data mt( 123 )/ -1190135011 / data mt( 124 )/ 1790562961 / data mt( 125 )/ -1493191723 / data mt( 126 )/ 475559465 / data mt( 127 )/ 69069 / c c c c it = is(1) leh = is(2) if (it .le. 0) then it = (it + it) .xor. mask else it = it + it endif nit = it - 1 c to ensure all-ones pattern omitted leh = leh * mt(nit .and. 127) + nit is(1) = it is(2) = leh if (leh .lt. 0) leh = .not. leh random = scale * float (leh .or. 1) return end c c c function irandm (is) c A random number generator called as a function by c random (iseed) or irandm (iseed) c The parameter is called by reference, not by value. c The routine(s) are callable directly as Fortran functions. c From C, I expect you have to pass a pointer to a 2-element vector c The first call gives a real uniform in 0 .. 1. c The second gives an integer uniform in 0 .. 2**31-1 c Both update iseed in exactly the same way. c iseed must be a 2-element integer vector. c The initial value of the second element may be anything. c c The period of the random sequence is 2**32 * (2**32-1) c The table mt(0:127) is defined by mt(i) = 69069 ** (128-i) c parameter (mask = 593970775) parameter (scale = 2.0 ** -31) c dimension is(2), mt(0:127) c data mt( 0 )/ 902906369 / data mt( 1 )/ 2030498053 / data mt( 2 )/ -473499623 / data mt( 3 )/ 1640834941 / data mt( 4 )/ 723406961 / data mt( 5 )/ 1993558325 / data mt( 6 )/ -257162999 / data mt( 7 )/ -1627724755 / data mt( 8 )/ 913952737 / data mt( 9 )/ 278845029 / data mt( 10 )/ 1327502073 / data mt( 11 )/ -1261253155 / data mt( 12 )/ 981676113 / data mt( 13 )/ -1785280363 / data mt( 14 )/ 1700077033 / data mt( 15 )/ 366908557 / data mt( 16 )/ -1514479167 / data mt( 17 )/ -682799163 / data mt( 18 )/ 141955545 / data mt( 19 )/ -830150595 / data mt( 20 )/ 317871153 / data mt( 21 )/ 1542036469 / data mt( 22 )/ -946413879 / data mt( 23 )/ -1950779155 / data mt( 24 )/ 985397153 / data mt( 25 )/ 626515237 / data mt( 26 )/ 530871481 / data mt( 27 )/ 783087261 / data mt( 28 )/ -1512358895 / data mt( 29 )/ 1031357269 / data mt( 30 )/ -2007710807 / data mt( 31 )/ -1652747955 / data mt( 32 )/ -1867214463 / data mt( 33 )/ 928251525 / data mt( 34 )/ 1243003801 / data mt( 35 )/ -2132510467 / data mt( 36 )/ 1874683889 / data mt( 37 )/ -717013323 / data mt( 38 )/ 218254473 / data mt( 39 )/ -1628774995 / data mt( 40 )/ -2064896159 / data mt( 41 )/ 69678053 / data mt( 42 )/ 281568889 / data mt( 43 )/ -2104168611 / data mt( 44 )/ -165128239 / data mt( 45 )/ 1536495125 / data mt( 46 )/ -39650967 / data mt( 47 )/ 546594317 / data mt( 48 )/ -725987007 / data mt( 49 )/ 1392966981 / data mt( 50 )/ 1044706649 / data mt( 51 )/ 687331773 / data mt( 52 )/ -2051306575 / data mt( 53 )/ 1544302965 / data mt( 54 )/ -758494647 / data mt( 55 )/ -1243934099 / data mt( 56 )/ -75073759 / data mt( 57 )/ 293132965 / data mt( 58 )/ -1935153095 / data mt( 59 )/ 118929437 / data mt( 60 )/ 807830417 / data mt( 61 )/ -1416222507 / data mt( 62 )/ -1550074071 / data mt( 63 )/ -84903219 / data mt( 64 )/ 1355292929 / data mt( 65 )/ -380482555 / data mt( 66 )/ -1818444007 / data mt( 67 )/ -204797315 / data mt( 68 )/ 170442609 / data mt( 69 )/ -1636797387 / data mt( 70 )/ 868931593 / data mt( 71 )/ -623503571 / data mt( 72 )/ 1711722209 / data mt( 73 )/ 381210981 / data mt( 74 )/ -161547783 / data mt( 75 )/ -272740131 / data mt( 76 )/ -1450066095 / data mt( 77 )/ 2116588437 / data mt( 78 )/ 1100682473 / data mt( 79 )/ 358442893 / data mt( 80 )/ -1529216831 / data mt( 81 )/ 2116152005 / data mt( 82 )/ -776333095 / data mt( 83 )/ 1265240893 / data mt( 84 )/ -482278607 / data mt( 85 )/ 1067190005 / data mt( 86 )/ 333444553 / data mt( 87 )/ 86502381 / data mt( 88 )/ 753481377 / data mt( 89 )/ 39000101 / data mt( 90 )/ 1779014585 / data mt( 91 )/ 219658653 / data mt( 92 )/ -920253679 / data mt( 93 )/ 2029538901 / data mt( 94 )/ 1207761577 / data mt( 95 )/ -1515772851 / data mt( 96 )/ -236195711 / data mt( 97 )/ 442620293 / data mt( 98 )/ 423166617 / data mt( 99 )/ -1763648515 / data mt( 100 )/ -398436623 / data mt( 101 )/ -1749358155 / data mt( 102 )/ -538598519 / data mt( 103 )/ -652439379 / data mt( 104 )/ 430550625 / data mt( 105 )/ -1481396507 / data mt( 106 )/ 2093206905 / data mt( 107 )/ -1934691747 / data mt( 108 )/ -962631983 / data mt( 109 )/ 1454463253 / data mt( 110 )/ -1877118871 / data mt( 111 )/ -291917555 / data mt( 112 )/ -1711673279 / data mt( 113 )/ 201201733 / data mt( 114 )/ -474645415 / data mt( 115 )/ -96764739 / data mt( 116 )/ -1587365199 / data mt( 117 )/ 1945705589 / data mt( 118 )/ 1303896393 / data mt( 119 )/ 1744831853 / data mt( 120 )/ 381957665 / data mt( 121 )/ 2135332261 / data mt( 122 )/ -55996615 / data mt( 123 )/ -1190135011 / data mt( 124 )/ 1790562961 / data mt( 125 )/ -1493191723 / data mt( 126 )/ 475559465 / data mt( 127 )/ 69069 / c c it = is(1) leh = is(2) if (it .le. 0) then it = (it + it) .xor. mask else it = it + it endif nit = it - 1 leh = leh * mt(nit .and. 127) + nit is(1) = it is(2) = leh c Want a positive result but dont want to lose top bit. if (leh .lt. 0) leh = .not. leh irandm = leh return end