Back to index

python3.2  3.2.2
Public Member Functions
test.test_random.TestBasicOps Class Reference
Inheritance diagram for test.test_random.TestBasicOps:
Inheritance graph
[legend]

List of all members.

Public Member Functions

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

Detailed Description

Definition at line 11 of file test_random.py.


Member Function Documentation

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:

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 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:

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:

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:

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:

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:

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:

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:


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