def test_avg_std(self): # Use integration to test distribution average and standard deviation. # Only works for distributions which do not consume variates in pairs g = random.Random() N = 5000 x = [i / float(N) for i in xrange(1, N)] for variate, args, mu, sigmasqrd in [ (g.uniform, (1.0, 10.0), (10.0 + 1.0) / 2, (10.0 - 1.0)**2 / 12), (g.triangular, (0.0, 1.0, 1.0 / 3.0), 4.0 / 9.0, 7.0 / 9.0 / 18.0), (g.expovariate, (1.5, ), 1 / 1.5, 1 / 1.5**2), (g.paretovariate, (5.0, ), 5.0 / (5.0 - 1), 5.0 / ((5.0 - 1)**2 * (5.0 - 2))), (g.weibullvariate, (1.0, 3.0), gamma(1 + 1 / 3.0), gamma(1 + 2 / 3.0) - gamma(1 + 1 / 3.0)**2) ]: g.random = x[:].pop y = [] for i in xrange(len(x)): try: y.append(variate(*args)) except IndexError: pass s1 = s2 = 0 for e in y: s1 += e s2 += (e - mu)**2 N = len(y) self.assertAlmostEqual(s1 / N, mu, 2) self.assertAlmostEqual(s2 / (N - 1), sigmasqrd, 2)
def test_ssn_2000(self): self.fake.random = random2.Random() self.fake.seed_instance(0) value = self.fake.ssn(min_age=0, max_age=1) assert re.search(r"^\d{11}$", value) assert value[0] in ("5", "6")
def test_zeroinputs(self): # Verify that distributions can handle a series of zero inputs' g = random.Random() x = [g.random() for i in xrange(50)] + [0.0] * 5 g.random = x[:].pop g.uniform(1, 10) g.random = x[:].pop g.paretovariate(1.0) g.random = x[:].pop g.expovariate(1.0) g.random = x[:].pop g.weibullvariate(1.0, 1.0) g.random = x[:].pop g.normalvariate(0.0, 1.0) g.random = x[:].pop g.gauss(0.0, 1.0) g.random = x[:].pop g.lognormvariate(0.0, 1.0) g.random = x[:].pop g.vonmisesvariate(0.0, 1.0) g.random = x[:].pop g.gammavariate(0.01, 1.0) g.random = x[:].pop g.gammavariate(1.0, 1.0) g.random = x[:].pop g.gammavariate(200.0, 1.0) g.random = x[:].pop g.betavariate(3.0, 3.0) g.random = x[:].pop g.triangular(0.0, 1.0, 1.0 / 3.0)
def test_ssn_2100(self): self.fake.random = random2.Random() self.fake.seed_instance(0) value = self.fake.ssn(min_age=0, max_age=1) assert re.search(r'^\d{11}$', value) assert value[0] in ('7', '8')
def __init__(self, seed, pattern=None): self.random = random.Random( generator.consistent_hash(seed + 'username')) self.firstNames = demographics.FirstNameGenerator(seed) self.lastNames = demographics.LastNameGenerator(seed) if pattern: self.pattern = pattern
def test_random_is_still_importable(self): g = {} r = random.Random(17) rnums = [r.randint(0, 999) for _ in range(100)] # With a seed, the results are predictable even from the random module safe_exec( "import random\n" "rnums = [random.randint(0, 999) for _ in xrange(100)]\n", g, random_seed=17) self.assertEqual(g['rnums'], rnums)
def client(client_num): # Deterministic random numbers so we can track down any failures. random = random2.Random(client_num) conn = self.database.open() try: for i in range(300): i = random.randint(1, 100) verify_a_blob(i, conn, 'r') verify_a_blob(i, conn, 'c') finally: conn.close()
def test_random_seeding(self): g = {} r = random.Random(17) rnums = [r.randint(0, 999) for _ in range(100)] # Without a seed, the results are unpredictable safe_exec("rnums = [random.randint(0, 999) for _ in xrange(100)]", g) self.assertNotEqual(g['rnums'], rnums) # With a seed, the results are predictable safe_exec("rnums = [random.randint(0, 999) for _ in xrange(100)]", g, random_seed=17) self.assertEqual(g['rnums'], rnums)
def test_ssn(self): self.fake.random = random2.Random() self.fake.seed_instance(0) value = self.fake.ssn() assert re.search(r'^\d{11}$', value) assert not value.endswith('0') self.fake.seed_instance(5) value = self.fake.ssn() assert re.search(r'^\d{11}$', value) assert value.endswith('0')
def _build_history(): random = random2.Random(42) history = [] serial = 2 for i in range(1000): serial += 1 oid = random.randint(i + 1000, i + 6000) history.append((b's', oid, serial, b'x' * random.randint(200, 2000))) for _ in range(10): oid = random.randint(i + 1000, i + 6000) history.append( (b'l', oid, serial, b'x' * random.randint(200, 2000))) return history
def __init__(self, seed): self.random = random.Random(generator.consistent_hash(seed+'address')) path = os.path.dirname(__file__) with io.open(os.path.join(path, self.streetNamesFile), 'r', encoding='latin-1') as file: self.streetNames = [e.strip() for e in file.readlines()] with io.open(os.path.join(path, self.streetPostfixFile), 'r', encoding='latin-1') as file: self.streetPostfix = [e.strip() for e in file.readlines()] with io.open(os.path.join(path, self.citiesFile), 'r', encoding='latin-1') as file: self.cities = [e.strip() for e in file.readlines()] with io.open(os.path.join(path, self.statesFile), 'r', encoding='latin-1') as file: self.states = [e.strip() for e in file.readlines()]
def test_invalid_ssn(self): self.fake.random = random2.Random() # Magic Numbers below generate '666-92-7944', '000-54-2963', '956-GG-9478', '436-00-1386', # and 134-76-0000 respectively. The "group" (GG) returned for '956-GG-9478 will be a random # number, and that random number is not in the "itin_group_numbers" List. The random GG occurs # even when using the same seed_instance() due to using random.choice() for GG to avoid valid # ITINs being returned as an invalid SSN: # # Ensure that generated SSNs are 11 characters long # including dashes, consist of dashes and digits only, and the tested number # violates the requirements below, ensuring an INVALID SSN is returned: # # A United States Social Security Number # (SSN) is a tax processing number issued by the Internal # Revenue Service with the format "AAA-GG-SSSS". The # number is divided into three parts: the first three # digits, known as the area number because they were # formerly assigned by geographical region; the middle two # digits, known as the group number; and the final four # digits, known as the serial number. SSNs with the # following characteristics are not allocated: # # 1) Numbers with all zeros in any digit group # (000-##-####, ###-00-####, ###-##-0000). # # 2) Numbers with 666 or 900-999 in the first digit group. # # https://en.wikipedia.org/wiki/Social_Security_number # # ITIN explained: # https://www.irs.gov/individuals/international-taxpayers/general-itin-information itin_group_numbers = [ 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 90, 91, 92, 94, 95, 96, 97, 98, 99] self.fake.seed_instance(1143) ssn = self.fake.ssn(taxpayer_identification_number_type='INVALID_SSN') assert len(ssn) == 11 assert ssn.replace('-', '').isdigit() assert ssn.startswith('666') self.fake.seed_instance(1514) ssn = self.fake.ssn(taxpayer_identification_number_type='INVALID_SSN') assert ssn.startswith('000') self.fake.seed_instance(2) ssn = self.fake.ssn(taxpayer_identification_number_type='INVALID_SSN') [area, group, serial] = ssn.split('-') assert 900 <= int(area) <= 999 and int(group) not in itin_group_numbers self.fake.seed_instance(9) ssn = self.fake.ssn(taxpayer_identification_number_type='INVALID_SSN') [area, group, serial] = ssn.split('-') assert int(area) < 900 and int(group) == 0 self.fake.seed_instance(1) ssn = self.fake.ssn(taxpayer_identification_number_type='INVALID_SSN') [area, group, serial] = ssn.split('-') assert int(area) < 900 and int(serial) == 0
class MersenneTwister_TestBasicOps(TestBasicOps): gen = random.Random() def test_setstate_first_arg(self): self.assertRaises(ValueError, self.gen.setstate, (1, None, None)) def test_setstate_middle_arg(self): # Wrong type, s/b tuple self.assertRaises(TypeError, self.gen.setstate, (2, None, None)) # Wrong length, s/b 625 self.assertRaises(ValueError, self.gen.setstate, (2, (1, 2, 3), None)) # Wrong type, s/b tuple of 625 ints self.assertRaises(TypeError, self.gen.setstate, (2, ('a', ) * 625, None)) # Last element s/b an int also self.assertRaises(TypeError, self.gen.setstate, (2, (0, ) * 624 + ('a', ), None)) def test_referenceImplementation(self): # Compare the python implementation with results from the original # code. Create 2000 53-bit precision random floats. Compare only # the last ten entries to show that the independent implementations # are tracking. Here is the main() function needed to create the # list of expected random numbers: # void main(void){ # int i; # unsigned long init[4]={61731, 24903, 614, 42143}, length=4; # init_by_array(init, length); # for (i=0; i<2000; i++) { # printf("%.15f ", genrand_res53()); # if (i%5==4) printf("\n"); # } # } expected = [ 0.45839803073713259, 0.86057815201978782, 0.92848331726782152, 0.35932681119782461, 0.081823493762449573, 0.14332226470169329, 0.084297823823520024, 0.53814864671831453, 0.089215024911993401, 0.78486196105372907 ] self.gen.seed(61731 + (24903 << 32) + (614 << 64) + (42143 << 96)) actual = self.randomlist(2000)[-10:] for a, e in zip(actual, expected): self.assertAlmostEqual(a, e, places=14) def test_strong_reference_implementation(self): # Like test_referenceImplementation, but checks for exact bit-level # equality. This should pass on any box where C double contains # at least 53 bits of precision (the underlying algorithm suffers # no rounding errors -- all results are exact). from math import ldexp expected = [ 0x0eab3258d2231f, 0x1b89db315277a5, 0x1db622a5518016, 0x0b7f9af0d575bf, 0x029e4c4db82240, 0x04961892f5d673, 0x02b291598e4589, 0x11388382c15694, 0x02dad977c9e1fe, 0x191d96d4d334c6 ] self.gen.seed(61731 + (24903 << 32) + (614 << 64) + (42143 << 96)) actual = self.randomlist(2000)[-10:] for a, e in zip(actual, expected): self.assertEqual(long(ldexp(a, 53)), e) def test_long_seed(self): # This is most interesting to run in debug mode, just to make sure # nothing blows up. Under the covers, a dynamically resized array # is allocated, consuming space proportional to the number of bits # in the seed. Unfortunately, that's a quadratic-time algorithm, # so don't make this horribly big. seed = (1 << (10000 * 8)) - 1 # about 10K bytes self.gen.seed(seed) def test_53_bits_per_float(self): # This should pass whenever a C double has 53 bit precision. span = 2**53 cum = 0 for i in xrange(100): cum |= int(self.gen.random() * span) self.assertEqual(cum, span - 1) def test_bigrand(self): # The randrange routine should build-up the required number of bits # in stages so that all bit positions are active. span = 2**500 cum = 0 for i in xrange(100): r = self.gen.randrange(span) self.assertTrue(0 <= r < span) cum |= r self.assertEqual(cum, span - 1) def test_bigrand_ranges(self): for i in [40, 80, 160, 200, 211, 250, 375, 512, 550]: start = self.gen.randrange(2**i) stop = self.gen.randrange(2**(i - 2)) if stop <= start: return self.assertTrue(start <= self.gen.randrange(start, stop) < stop) def test_rangelimits(self): for start, stop in [(-2, 0), (-(2**60) - 2, -(2**60)), (2**60, 2**60 + 2)]: self.assertEqual( set(range(start, stop)), set([self.gen.randrange(start, stop) for i in xrange(100)])) def test_genrandbits(self): # Verify cross-platform repeatability self.gen.seed(1234567) self.assertEqual(self.gen.getrandbits(100), 97904845777343510404718956115) # Verify ranges for k in xrange(1, 1000): self.assertTrue(0 <= self.gen.getrandbits(k) < 2**k) # Verify all bits active getbits = self.gen.getrandbits for span in [1, 2, 3, 4, 31, 32, 32, 52, 53, 54, 119, 127, 128, 129]: cum = 0 for i in xrange(100): cum |= getbits(span) self.assertEqual(cum, 2**span - 1) # Verify argument checking self.assertRaises(TypeError, self.gen.getrandbits) self.assertRaises(TypeError, self.gen.getrandbits, 'a') self.assertRaises(TypeError, self.gen.getrandbits, 1, 2) if sys.version_info < (3, 9): self.assertRaises(ValueError, self.gen.getrandbits, 0) self.assertRaises(ValueError, self.gen.getrandbits, -1) def test_randbelow_logic(self, _log=log, int=int): # check bitcount transition points: 2**i and 2**(i+1)-1 # show that: k = int(1.001 + _log(n, 2)) # is equal to or one greater than the number of bits in n for i in xrange(1, 1000): n = 1 << i # check an exact power of two numbits = i + 1 k = int(1.00001 + _log(n, 2)) self.assertEqual(k, numbits) self.assertTrue(n == 2**(k - 1)) n += n - 1 # check 1 below the next power of two k = int(1.00001 + _log(n, 2)) self.assertTrue(k in [numbits, numbits + 1]) self.assertTrue(2**k > n > 2**(k - 2)) n -= n >> 15 # check a little farther below the next power of two k = int(1.00001 + _log(n, 2)) self.assertEqual(k, numbits) # note the stronger assertion self.assertTrue( 2**k > n > 2**(k - 1)) # note the stronger assertion def test_randrange_bug_1590891(self): start = 1000000000000 stop = -100000000000000000000 step = -200 x = self.gen.randrange(start, stop, step) self.assertTrue(stop < x <= start) self.assertEqual((x + stop) % step, 0)
def __init__(self, seed): self.random = random.Random(generator.consistent_hash(seed+'ssn'))
def __init__(self, seed, fivesAreaCode=False): self.random = random.Random(generator.consistent_hash(seed+'phone')) self.fivesAreaCode = fivesAreaCode
def __init__(self, seed): self.random = random.Random(generator.consistent_hash(seed + 'enail')) self.usernames = UsernameDataGenerator(seed) self.words = generator.TextDataGenerator(seed, self.wordsFile) self.tlds = generator.CSVDataGenerator(seed, self.tldsFile)