     if bound.isPower2 {
         _rand &= bound &- 1
     } else {
-        let limit = RANDMAX - RANDMAX % bound
+        let limit = RANDMAX - mod(RANDMAX, bound)
         while _rand >= limit {
             sec_random(&_rand, sizeof(UIntMax))
         }
-        _rand %= bound
+        _rand = mod(_rand, bound)
     }
     return _rand
 }


@warn_unused_result
public func mulmod(a: UInt8, _ b: UInt8, _ m: UInt8) -> UInt8 {
    return UInt8((UInt16(a) &* UInt16(b)) % UInt16(m))
}
@warn_unused_result
public func mulmod(a: UInt16, _ b: UInt16, _ m: UInt16) -> UInt16 {
    return UInt16(truncatingBitPattern: (UInt32(a) &* UInt32(b)) % UInt32(m))
}
@warn_unused_result
public func mulmod(a: UInt32, _ b: UInt32, _ m: UInt32) -> UInt32 {
    return UInt32(truncatingBitPattern: (UInt64(a) &* UInt64(b)) % UInt64(m))
}

@warn_unused_result

    var x: (U, U) = (1, 0)
    while b != 0 {
        x = (x.1, x.0 + (a / b) * x.1)
        (a, b) = (b, mod(a, b))
        iter += 1
    }
    if a != 1 {
