src/dlt/randomizer_utils.py
def python_hash(SSN: int) -> int:
    """
    A pythonic implementation of COBOL code using floating-point arithmetic. Note that
    this will differ ever-so-slightly from the cobol_hash due to the differing rounding
    conventions.
    """
    # Constants determined by DoIT
    L_SD = SSN
    C_Q = 127773  # 3^2 * 14197
    C_A = 16807  # 7^5
    C_R = 2836  # 2^2 * 709
    C_M = 2147483647  # prime (In fact, 2^{2^5 - 1} - 1, double Mersenne)

    # Translated
    W_HI = L_SD // C_Q
    W_LO = L_SD % C_Q

    # Recombine the quotient and remainder mod a medium-sized almost-prime with two
    # coprime factors. (N.B. Not sure exactly why C_A is a power of 7 whereas C_R is
    # almost prime. Would be curious to read the history of this algorithm.)
    L_SD = C_A * W_LO - C_R * W_HI

    # Note that C_M is _almost_ 2^31, but not quite. Also, note that
    # C_A * W_LO - C_R * W_HI is maximized when SSN = C_Q - 1
    # and it is minimized when SSN is the largest social security number which is
    # exactly divisible by C_Q, i.e., (999_99_9999 // C_Q) * C_Q = 999_95_1498.
    #
    # In either case, C_A * W_LO - C_R * W_HI \in (-C_M, C_M) and so the following
    # block guarantees that L_SD will be in [0, C_M).
    #
    # We also note that the _smallest negative_ value that C_A * W_LO - C_R * W_HI can
    # achieve in theory is -1 (since C_A and C_R are coprime) but I haven't done the
    # computation to determine whether it's actually possible in this range of numbers
    if L_SD <= 0:
        warnings.warn("L_SD is negative")
        L_SD += C_M

    # And so by the above comment, L_RAND is in [0, 1) and this rounding gives us the
    # top 10 digits of the mantissa
    L_RAND = math.floor(L_SD / C_M * 1e10) / 1e10

    return L_RAND

    A pythonic implementation of COBOL code using floating-point arithmetic. Note that
    this will differ ever-so-slightly from the cobol_hash due to the differing rounding
    conventions.
    
A pythonic implementation of COBOL code using floating-point arithmetic. Note that
this will differ ever-so-slightly from the cobol_hash due to the differing rounding
conventions.
A pythonic implementation of COBOL code using floating - point arithmetic . Note that this will differ ever - so - slightly from the cobol_hash due to the differing rounding conventions .
def python_hash(SSN: int) -> int:
    L_SD = SSN
    C_Q = 127773  
    C_A = 16807  
    C_R = 2836  
    C_M = 2147483647  
    W_HI = L_SD // C_Q
    W_LO = L_SD % C_Q
    L_SD = C_A * W_LO - C_R * W_HI
     In either case, C_A * W_LO - C_R * W_HI \in (-C_M, C_M) and so the following
     block guarantees that L_SD will be in [0, C_M).
     We also note that the _smallest negative_ value that C_A * W_LO - C_R * W_HI can
     achieve in theory is -1 (since C_A and C_R are coprime) but I haven't done the
     computation to determine whether it's actually possible in this range of numbers
    if L_SD <= 0:
        warnings.warn("L_SD is negative")
        L_SD += C_M
     And so by the above comment, L_RAND is in [0, 1) and this rounding gives us the
     top 10 digits of the mantissa
    L_RAND = math.floor(L_SD / C_M * 1e10) / 1e10
    return L_RAND
def python_hash ( SSN : int ) -> int : L_SD = SSN C_Q = 127773 C_A = 16807 C_R = 2836 C_M = 2147483647 W_HI = L_SD // C_Q W_LO = L_SD % C_Q L_SD = C_A * W_LO - C_R * W_HI if L_SD <= 0 : warnings . warn ( "L_SD is negative" ) L_SD += C_M L_RAND = math . floor ( L_SD / C_M * 1e10 ) / 1e10 return L_RAND
A pythonic implementation of COBOL code using floating-point arithmetic.
A pythonic implementation of COBOL code using floating - point arithmetic .
"""
    A pythonic implementation of COBOL code using floating-point arithmetic. Note that
    this will differ ever-so-slightly from the cobol_hash due to the differing rounding
    conventions.
    """ # Constants determined by DoIT # 3^2 * 14197 # 7^5 # 2^2 * 709 # prime (In fact, 2^{2^5 - 1} - 1, double Mersenne) # Translated # Recombine the quotient and remainder mod a medium-sized almost-prime with two # coprime factors. (N.B. Not sure exactly why C_A is a power of 7 whereas C_R is # almost prime. Would be curious to read the history of this algorithm.) # Note that C_M is _almost_ 2^31, but not quite. Also, note that # C_A * W_LO - C_R * W_HI is maximized when SSN = C_Q - 1 # and it is minimized when SSN is the largest social security number which is # exactly divisible by C_Q, i.e., (999_99_9999 // C_Q) * C_Q = 999_95_1498. # # In either case, C_A * W_LO - C_R * W_HI \in (-C_M, C_M) and so the following # block guarantees that L_SD will be in [0, C_M). # # We also note that the _smallest negative_ value that C_A * W_LO - C_R * W_HI can # achieve in theory is -1 (since C_A and C_R are coprime) but I haven't done the # computation to determine whether it's actually possible in this range of numbers # And so by the above comment, L_RAND is in [0, 1) and this rounding gives us the # top 10 digits of the mantissa
SSN
int