Back to index

python3.2  3.2.2
Public Member Functions | Static Public Attributes
test.test_random.MersenneTwister_TestBasicOps Class Reference
Inheritance diagram for test.test_random.MersenneTwister_TestBasicOps:
Inheritance graph
[legend]
Collaboration diagram for test.test_random.MersenneTwister_TestBasicOps:
Collaboration graph
[legend]

List of all members.

Public Member Functions

def test_guaranteed_stable
def test_setstate_first_arg
def test_setstate_middle_arg
def test_referenceImplementation
def test_strong_reference_implementation
def test_long_seed
def test_53_bits_per_float
def test_bigrand
def test_bigrand_ranges
def test_rangelimits
def test_genrandbits
def test_randbelow_logic
def test_randrange_bug_1590891
def randomlist
def test_autoseed
def test_saverestore
def test_seedargs
def test_choice
def test_sample
def test_sample_distribution
def test_sample_inputs
def test_sample_on_dicts
def test_gauss
def test_pickling
def test_bug_1727780
def test_bug_9025

Static Public Attributes

tuple gen = random.Random()

Detailed Description

Definition at line 238 of file test_random.py.


Member Function Documentation

def test.test_random.TestBasicOps.randomlist (   self,
  n 
) [inherited]
Helper function to make a list of random numbers

Definition at line 16 of file test_random.py.

00016 
00017     def randomlist(self, n):
00018         """Helper function to make a list of random numbers"""
00019         return [self.gen.random() for i in range(n)]

Here is the caller graph for this function:

Definition at line 327 of file test_random.py.

00327 
00328     def test_53_bits_per_float(self):
00329         # This should pass whenever a C double has 53 bit precision.
00330         span = 2 ** 53
00331         cum = 0
00332         for i in range(100):
00333             cum |= int(self.gen.random() * span)
00334         self.assertEqual(cum, span-1)

Here is the call graph for this function:

def test.test_random.TestBasicOps.test_autoseed (   self) [inherited]

Reimplemented in test.test_random.SystemRandom_TestBasicOps.

Definition at line 20 of file test_random.py.

00020 
00021     def test_autoseed(self):
00022         self.gen.seed()
00023         state1 = self.gen.getstate()
00024         time.sleep(0.1)
00025         self.gen.seed()      # diffent seeds at different times
00026         state2 = self.gen.getstate()
00027         self.assertNotEqual(state1, state2)

Here is the call graph for this function:

Definition at line 335 of file test_random.py.

00335 
00336     def test_bigrand(self):
00337         # The randrange routine should build-up the required number of bits
00338         # in stages so that all bit positions are active.
00339         span = 2 ** 500
00340         cum = 0
00341         for i in range(100):
00342             r = self.gen.randrange(span)
00343             self.assertTrue(0 <= r < span)
00344             cum |= r
00345         self.assertEqual(cum, span-1)

Here is the call graph for this function:

Definition at line 346 of file test_random.py.

00346 
00347     def test_bigrand_ranges(self):
00348         for i in [40,80, 160, 200, 211, 250, 375, 512, 550]:
00349             start = self.gen.randrange(2 ** i)
00350             stop = self.gen.randrange(2 ** (i-2))
00351             if stop <= start:
00352                 return
00353             self.assertTrue(start <= self.gen.randrange(start, stop) < stop)

Here is the call graph for this function:

Definition at line 120 of file test_random.py.

00120 
00121     def test_bug_1727780(self):
00122         # verify that version-2-pickles can be loaded
00123         # fine, whether they are created on 32-bit or 64-bit
00124         # platforms, and that version-3-pickles load fine.
00125         files = [("randv2_32.pck", 780),
00126                  ("randv2_64.pck", 866),
00127                  ("randv3.pck", 343)]
00128         for file, value in files:
00129             f = open(support.findfile(file),"rb")
00130             r = pickle.load(f)
00131             f.close()
00132             self.assertEqual(int(r.random()*1000), value)

Here is the call graph for this function:

def test.test_random.TestBasicOps.test_bug_9025 (   self) [inherited]

Definition at line 133 of file test_random.py.

00133 
00134     def test_bug_9025(self):
00135         # Had problem with an uneven distribution in int(n*random())
00136         # Verify the fix by checking that distributions fall within expectations.
00137         n = 100000
00138         randrange = self.gen.randrange
00139         k = sum(randrange(6755399441055744) % 3 == 2 for i in range(n))
00140         self.assertTrue(0.30 < k/n < .37, (k/n))

Here is the call graph for this function:

def test.test_random.TestBasicOps.test_choice (   self) [inherited]

Definition at line 45 of file test_random.py.

00045 
00046     def test_choice(self):
00047         choice = self.gen.choice
00048         with self.assertRaises(IndexError):
00049             choice([])
00050         self.assertEqual(choice([50]), 50)
00051         self.assertIn(choice([25, 75]), [25, 75])

Here is the call graph for this function:

def test.test_random.TestBasicOps.test_gauss (   self) [inherited]

Reimplemented in test.test_random.SystemRandom_TestBasicOps.

Definition at line 96 of file test_random.py.

00096 
00097     def test_gauss(self):
00098         # Ensure that the seed() method initializes all the hidden state.  In
00099         # particular, through 2.2.1 it failed to reset a piece of state used
00100         # by (and only by) the .gauss() method.
00101 
00102         for seed in 1, 12, 123, 1234, 12345, 123456, 654321:
00103             self.gen.seed(seed)
00104             x1 = self.gen.random()
00105             y1 = self.gen.gauss(0, 1)
00106 
00107             self.gen.seed(seed)
00108             x2 = self.gen.random()
00109             y2 = self.gen.gauss(0, 1)
00110 
00111             self.assertEqual(x1, x2)
00112             self.assertEqual(y1, y2)

Here is the call graph for this function:

Definition at line 359 of file test_random.py.

00359 
00360     def test_genrandbits(self):
00361         # Verify cross-platform repeatability
00362         self.gen.seed(1234567)
00363         self.assertEqual(self.gen.getrandbits(100),
00364                          97904845777343510404718956115)
00365         # Verify ranges
00366         for k in range(1, 1000):
00367             self.assertTrue(0 <= self.gen.getrandbits(k) < 2**k)
00368 
00369         # Verify all bits active
00370         getbits = self.gen.getrandbits
00371         for span in [1, 2, 3, 4, 31, 32, 32, 52, 53, 54, 119, 127, 128, 129]:
00372             cum = 0
00373             for i in range(100):
00374                 cum |= getbits(span)
00375             self.assertEqual(cum, 2**span-1)
00376 
00377         # Verify argument checking
00378         self.assertRaises(TypeError, self.gen.getrandbits)
00379         self.assertRaises(TypeError, self.gen.getrandbits, 'a')
00380         self.assertRaises(TypeError, self.gen.getrandbits, 1, 2)
00381         self.assertRaises(ValueError, self.gen.getrandbits, 0)
00382         self.assertRaises(ValueError, self.gen.getrandbits, -1)

Here is the call graph for this function:

Definition at line 241 of file test_random.py.

00241 
00242     def test_guaranteed_stable(self):
00243         # These sequences are guaranteed to stay the same across versions of python
00244         self.gen.seed(3456147, version=1)
00245         self.assertEqual([self.gen.random().hex() for i in range(4)],
00246             ['0x1.ac362300d90d2p-1', '0x1.9d16f74365005p-1',
00247              '0x1.1ebb4352e4c4dp-1', '0x1.1a7422abf9c11p-1'])
00248         self.gen.seed("the quick brown fox", version=2)
00249         self.assertEqual([self.gen.random().hex() for i in range(4)],
00250             ['0x1.1239ddfb11b7cp-3', '0x1.b3cbb5c51b120p-4',
00251              '0x1.8c4f55116b60fp-1', '0x1.63eb525174a27p-1'])

Here is the call graph for this function:

Definition at line 318 of file test_random.py.

00318 
00319     def test_long_seed(self):
00320         # This is most interesting to run in debug mode, just to make sure
00321         # nothing blows up.  Under the covers, a dynamically resized array
00322         # is allocated, consuming space proportional to the number of bits
00323         # in the seed.  Unfortunately, that's a quadratic-time algorithm,
00324         # so don't make this horribly big.
00325         seed = (1 << (10000 * 8)) - 1  # about 10K bytes
00326         self.gen.seed(seed)

def test.test_random.TestBasicOps.test_pickling (   self) [inherited]

Reimplemented in test.test_random.SystemRandom_TestBasicOps.

Definition at line 113 of file test_random.py.

00113 
00114     def test_pickling(self):
00115         state = pickle.dumps(self.gen)
00116         origseq = [self.gen.random() for i in range(10)]
00117         newgen = pickle.loads(state)
00118         restoredseq = [newgen.random() for i in range(10)]
00119         self.assertEqual(origseq, restoredseq)

Here is the call graph for this function:

def test.test_random.MersenneTwister_TestBasicOps.test_randbelow_logic (   self,
  _log = log,
  int = int 
)

Definition at line 383 of file test_random.py.

00383 
00384     def test_randbelow_logic(self, _log=log, int=int):
00385         # check bitcount transition points:  2**i and 2**(i+1)-1
00386         # show that: k = int(1.001 + _log(n, 2))
00387         # is equal to or one greater than the number of bits in n
00388         for i in range(1, 1000):
00389             n = 1 << i # check an exact power of two
00390             numbits = i+1
00391             k = int(1.00001 + _log(n, 2))
00392             self.assertEqual(k, numbits)
00393             self.assertEqual(n, 2**(k-1))
00394 
00395             n += n - 1      # check 1 below the next power of two
00396             k = int(1.00001 + _log(n, 2))
00397             self.assertIn(k, [numbits, numbits+1])
00398             self.assertTrue(2**k > n > 2**(k-2))
00399 
00400             n -= n >> 15     # check a little farther below the next power of two
00401             k = int(1.00001 + _log(n, 2))
00402             self.assertEqual(k, numbits)        # note the stronger assertion
00403             self.assertTrue(2**k > n > 2**(k-1))   # note the stronger assertion

Here is the call graph for this function:

Definition at line 404 of file test_random.py.

00404 
00405     def test_randrange_bug_1590891(self):
00406         start = 1000000000000
00407         stop = -100000000000000000000
00408         step = -200
00409         x = self.gen.randrange(start, stop, step)
00410         self.assertTrue(stop < x <= start)
00411         self.assertEqual((x+stop)%step, 0)

Here is the call graph for this function:

Definition at line 354 of file test_random.py.

00354 
00355     def test_rangelimits(self):
00356         for start, stop in [(-2,0), (-(2**60)-2,-(2**60)), (2**60,2**60+2)]:
00357             self.assertEqual(set(range(start,stop)),
00358                 set([self.gen.randrange(start,stop) for i in range(100)]))

Here is the call graph for this function:

Definition at line 265 of file test_random.py.

00265 
00266     def test_referenceImplementation(self):
00267         # Compare the python implementation with results from the original
00268         # code.  Create 2000 53-bit precision random floats.  Compare only
00269         # the last ten entries to show that the independent implementations
00270         # are tracking.  Here is the main() function needed to create the
00271         # list of expected random numbers:
00272         #    void main(void){
00273         #         int i;
00274         #         unsigned long init[4]={61731, 24903, 614, 42143}, length=4;
00275         #         init_by_array(init, length);
00276         #         for (i=0; i<2000; i++) {
00277         #           printf("%.15f ", genrand_res53());
00278         #           if (i%5==4) printf("\n");
00279         #         }
00280         #     }
00281         expected = [0.45839803073713259,
00282                     0.86057815201978782,
00283                     0.92848331726782152,
00284                     0.35932681119782461,
00285                     0.081823493762449573,
00286                     0.14332226470169329,
00287                     0.084297823823520024,
00288                     0.53814864671831453,
00289                     0.089215024911993401,
00290                     0.78486196105372907]
00291 
00292         self.gen.seed(61731 + (24903<<32) + (614<<64) + (42143<<96))
00293         actual = self.randomlist(2000)[-10:]
00294         for a, e in zip(actual, expected):
00295             self.assertAlmostEqual(a,e,places=14)

Here is the call graph for this function:

def test.test_random.TestBasicOps.test_sample (   self) [inherited]

Definition at line 52 of file test_random.py.

00052 
00053     def test_sample(self):
00054         # For the entire allowable range of 0 <= k <= N, validate that
00055         # the sample is of the correct length and contains only unique items
00056         N = 100
00057         population = range(N)
00058         for k in range(N+1):
00059             s = self.gen.sample(population, k)
00060             self.assertEqual(len(s), k)
00061             uniq = set(s)
00062             self.assertEqual(len(uniq), k)
00063             self.assertTrue(uniq <= set(population))
00064         self.assertEqual(self.gen.sample([], 0), [])  # test edge case N==k==0

Here is the call graph for this function:

Definition at line 65 of file test_random.py.

00065 
00066     def test_sample_distribution(self):
00067         # For the entire allowable range of 0 <= k <= N, validate that
00068         # sample generates all possible permutations
00069         n = 5
00070         pop = range(n)
00071         trials = 10000  # large num prevents false negatives without slowing normal case
00072         def factorial(n):
00073             if n == 0:
00074                 return 1
00075             return n * factorial(n - 1)
00076         for k in range(n):
00077             expected = factorial(n) // factorial(n-k)
00078             perms = {}
00079             for i in range(trials):
00080                 perms[tuple(self.gen.sample(pop, k))] = None
00081                 if len(perms) == expected:
00082                     break
00083             else:
00084                 self.fail()

Here is the call graph for this function:

Definition at line 85 of file test_random.py.

00085 
00086     def test_sample_inputs(self):
00087         # SF bug #801342 -- population can be any iterable defining __len__()
00088         self.gen.sample(set(range(20)), 2)
00089         self.gen.sample(range(20), 2)
00090         self.gen.sample(range(20), 2)
00091         self.gen.sample(str('abcdefghijklmnopqrst'), 2)
00092         self.gen.sample(tuple('abcdefghijklmnopqrst'), 2)

Here is the call graph for this function:

Definition at line 93 of file test_random.py.

00093 
00094     def test_sample_on_dicts(self):
00095         self.assertRaises(TypeError, self.gen.sample, dict.fromkeys('abcdef'), 2)

Here is the call graph for this function:

Reimplemented in test.test_random.SystemRandom_TestBasicOps.

Definition at line 28 of file test_random.py.

00028 
00029     def test_saverestore(self):
00030         N = 1000
00031         self.gen.seed()
00032         state = self.gen.getstate()
00033         randseq = self.randomlist(N)
00034         self.gen.setstate(state)    # should regenerate the same sequence
00035         self.assertEqual(randseq, self.randomlist(N))

Here is the call graph for this function:

def test.test_random.TestBasicOps.test_seedargs (   self) [inherited]

Reimplemented in test.test_random.SystemRandom_TestBasicOps.

Definition at line 36 of file test_random.py.

00036 
00037     def test_seedargs(self):
00038         for arg in [None, 0, 0, 1, 1, -1, -1, 10**20, -(10**20),
00039                     3.14, 1+2j, 'a', tuple('abc')]:
00040             self.gen.seed(arg)
00041         for arg in [list(range(3)), dict(one=1)]:
00042             self.assertRaises(TypeError, self.gen.seed, arg)
00043         self.assertRaises(TypeError, self.gen.seed, 1, 2, 3, 4)
00044         self.assertRaises(TypeError, type(self.gen), [])

Here is the call graph for this function:

Definition at line 252 of file test_random.py.

00252 
00253     def test_setstate_first_arg(self):
00254         self.assertRaises(ValueError, self.gen.setstate, (1, None, None))

Here is the call graph for this function:

Definition at line 255 of file test_random.py.

00255 
00256     def test_setstate_middle_arg(self):
00257         # Wrong type, s/b tuple
00258         self.assertRaises(TypeError, self.gen.setstate, (2, None, None))
00259         # Wrong length, s/b 625
00260         self.assertRaises(ValueError, self.gen.setstate, (2, (1,2,3), None))
00261         # Wrong type, s/b tuple of 625 ints
00262         self.assertRaises(TypeError, self.gen.setstate, (2, ('a',)*625, None))
00263         # Last element s/b an int also
00264         self.assertRaises(TypeError, self.gen.setstate, (2, (0,)*624+('a',), None))

Here is the call graph for this function:

Definition at line 296 of file test_random.py.

00296 
00297     def test_strong_reference_implementation(self):
00298         # Like test_referenceImplementation, but checks for exact bit-level
00299         # equality.  This should pass on any box where C double contains
00300         # at least 53 bits of precision (the underlying algorithm suffers
00301         # no rounding errors -- all results are exact).
00302         from math import ldexp
00303 
00304         expected = [0x0eab3258d2231f,
00305                     0x1b89db315277a5,
00306                     0x1db622a5518016,
00307                     0x0b7f9af0d575bf,
00308                     0x029e4c4db82240,
00309                     0x04961892f5d673,
00310                     0x02b291598e4589,
00311                     0x11388382c15694,
00312                     0x02dad977c9e1fe,
00313                     0x191d96d4d334c6]
00314         self.gen.seed(61731 + (24903<<32) + (614<<64) + (42143<<96))
00315         actual = self.randomlist(2000)[-10:]
00316         for a, e in zip(actual, expected):
00317             self.assertEqual(int(ldexp(a, 53)), e)

Here is the call graph for this function:


Member Data Documentation

Definition at line 239 of file test_random.py.


The documentation for this class was generated from the following file: