@@ -223,33 +223,6 @@ def test_sample_with_counts(self):
223
223
with self .assertRaises (ValueError ):
224
224
sample (['red' , 'green' , 'blue' ], counts = [1 , 2 , 3 , 4 ], k = 2 ) # too many counts
225
225
226
- def test_sample_counts_equivalence (self ):
227
- # Test the documented strong equivalence to a sample with repeated elements.
228
- # We run this test on random.Random() which makes deterministic selections
229
- # for a given seed value.
230
- sample = random .sample
231
- seed = random .seed
232
-
233
- colors = ['red' , 'green' , 'blue' , 'orange' , 'black' , 'amber' ]
234
- counts = [500 , 200 , 20 , 10 , 5 , 1 ]
235
- k = 700
236
- seed (8675309 )
237
- s1 = sample (colors , counts = counts , k = k )
238
- seed (8675309 )
239
- expanded = [color for (color , count ) in zip (colors , counts ) for i in range (count )]
240
- self .assertEqual (len (expanded ), sum (counts ))
241
- s2 = sample (expanded , k = k )
242
- self .assertEqual (s1 , s2 )
243
-
244
- pop = 'abcdefghi'
245
- counts = [10 , 9 , 8 , 7 , 6 , 5 , 4 , 3 , 2 ]
246
- seed (8675309 )
247
- s1 = '' .join (sample (pop , counts = counts , k = 30 ))
248
- expanded = '' .join ([letter for (letter , count ) in zip (pop , counts ) for i in range (count )])
249
- seed (8675309 )
250
- s2 = '' .join (sample (expanded , k = 30 ))
251
- self .assertEqual (s1 , s2 )
252
-
253
226
def test_choices (self ):
254
227
choices = self .gen .choices
255
228
data = ['red' , 'green' , 'blue' , 'yellow' ]
@@ -957,6 +930,33 @@ def test_randbytes_getrandbits(self):
957
930
self .assertEqual (self .gen .randbytes (n ),
958
931
gen2 .getrandbits (n * 8 ).to_bytes (n , 'little' ))
959
932
933
+ def test_sample_counts_equivalence (self ):
934
+ # Test the documented strong equivalence to a sample with repeated elements.
935
+ # We run this test on random.Random() which makes deterministic selections
936
+ # for a given seed value.
937
+ sample = self .gen .sample
938
+ seed = self .gen .seed
939
+
940
+ colors = ['red' , 'green' , 'blue' , 'orange' , 'black' , 'amber' ]
941
+ counts = [500 , 200 , 20 , 10 , 5 , 1 ]
942
+ k = 700
943
+ seed (8675309 )
944
+ s1 = sample (colors , counts = counts , k = k )
945
+ seed (8675309 )
946
+ expanded = [color for (color , count ) in zip (colors , counts ) for i in range (count )]
947
+ self .assertEqual (len (expanded ), sum (counts ))
948
+ s2 = sample (expanded , k = k )
949
+ self .assertEqual (s1 , s2 )
950
+
951
+ pop = 'abcdefghi'
952
+ counts = [10 , 9 , 8 , 7 , 6 , 5 , 4 , 3 , 2 ]
953
+ seed (8675309 )
954
+ s1 = '' .join (sample (pop , counts = counts , k = 30 ))
955
+ expanded = '' .join ([letter for (letter , count ) in zip (pop , counts ) for i in range (count )])
956
+ seed (8675309 )
957
+ s2 = '' .join (sample (expanded , k = 30 ))
958
+ self .assertEqual (s1 , s2 )
959
+
960
960
961
961
def gamma (z , sqrt2pi = (2.0 * pi )** 0.5 ):
962
962
# Reflection to right half of complex plane
0 commit comments