def testpartial(self):
     def odd(n):
         """
         Return None for even numbers.
         """
         if n % 2:
             return n
     self.assertEqual(1, bigrandom.map_choice(odd, 2**100) % 2)
示例#2
0
def randPrime(n):
    """
    Return a random n-digits prime
    """
    if n <= 0:
        raise ValueError("input number must be natural number")

    if n == 1:
        return bigrandom.map_choice(lambda i: (2, 3, 5, 7)[i], 4)

    p = bigrandom.randrange(10**(n-1)+1, 10**n, 2)
    while not primeq(p):
        p += 2
    if p < 10**n:
        return p

    # in very rare case or n is too small case,
    # search continues from the lower bound.
    p = 10**(n-1) + 1
    while not primeq(p):
        p += 2
    return p
示例#3
0
def randPrime(n):
    """
    Return a random n-digits prime
    """
    if n <= 0:
        raise ValueError("input number must be natural number")

    if n == 1:
        return bigrandom.map_choice(lambda i: (2, 3, 5, 7)[i], 4)

    p = bigrandom.randrange(10**(n - 1) + 1, 10**n, 2)
    while not primeq(p):
        p += 2
    if p < 10**n:
        return p

    # in very rare case or n is too small case,
    # search continues from the lower bound.
    p = 10**(n - 1) + 1
    while not primeq(p):
        p += 2
    return p
示例#4
0
 def testeven(self):
     double = lambda x: x + x
     self.assertTrue(0 <= bigrandom.map_choice(double, 2**100) < 2**101)
     self.assertEqual(0, bigrandom.map_choice(double, 2**100) % 2)
示例#5
0
 def testidentity(self):
     i = lambda x: x
     self.assertTrue(0 <= bigrandom.map_choice(i, 2**100) < 2**100)