!********************************************************************************************************************************** ! ISROUND ! ! Statistical rounding function. ! ! Given a positive real number X of the form I.F, where I is the integer part and F is the fractional part, ! round the result to integer I with probablility 1-F, or to integer I+1 with probability F. ! ! This version returns an INTEGER result. ! ! The random number generator should be initialized by calling RANDOM_SEED in the main program. !********************************************************************************************************************************** FUNCTION ISROUND (X) RESULT (IY) IMPLICIT NONE DOUBLE PRECISION, INTENT(IN) :: X INTEGER :: IY INTEGER :: I REAL :: XRAND DOUBLE PRECISION :: X1, F LOGICAL :: NEG NEG = X .LT. 0.0D0 ! save sign of original X, in case X is negative X1 = ABS(X) ! X1 = |X| I = INT(X1) ! I = integer part of X1 F = X1 - I ! F = fractional part of X1 CALL RANDOM_NUMBER (XRAND) ! random number between 0 and 1 IF (XRAND .GT. F) THEN ! with probability 1-F.. IY = I ! round down ELSE ! with probability F.. IY = I+1 ! round up END IF IF (NEG) IY = -IY ! restore original sign RETURN END FUNCTION ISROUND