Skip to content

Commit f114bd5

Browse files
authored
Merge pull request #761 from drvinceknight/760
Stop random_choice sampling when used with 0 or 1
2 parents 10b3123 + 13969dd commit f114bd5

File tree

5 files changed

+34
-6
lines changed

5 files changed

+34
-6
lines changed

axelrod/random_.py

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,23 @@ def random_choice(p=0.5):
88
Return 'C' with probability `p`, else return 'D'
99
1010
Emulates Python's random.choice(['C', 'D']) since it is not consistent
11-
across Python 2.7 to Python 3.4"""
11+
across Python 2.7 to Python 3.4
12+
13+
Parameters
14+
----------
15+
16+
p : float
17+
The probability of picking 'C'
18+
19+
Returns
20+
-------
21+
axelrod.Actions.C or axelrod.Actions.D
22+
"""
23+
if p == 0:
24+
return Actions.D
25+
26+
if p == 1:
27+
return Actions.C
1228

1329
r = random.random()
1430
if r < p:

axelrod/tests/unit/test_axelrod_second.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ def test_strategy(self):
8080
# Test defection after opponent defection
8181
self.responses_test([D], [D], [D])
8282
self.responses_test([D, D], [D, D], [D])
83-
self.responses_test([D, C, C, D], [D, C, C, D], [D, C], random_seed=8)
83+
self.responses_test([D, C, C, D], [D, C, C, D], [C, C], random_seed=8)
8484

8585

8686
class TestTester(TestPlayer):

axelrod/tests/unit/test_headsup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ def test_rounds(self):
3838
class TestZDGTFT2vsBully(TestHeadsUp):
3939
"""Test ZDGTFT2 vs Bully"""
4040
def test_rounds(self):
41-
self.versus_test(axelrod.ZDGTFT2(), axelrod.Bully(), [C, D, D, C, C, D],
41+
self.versus_test(axelrod.ZDGTFT2(), axelrod.Bully(), [C, D, D, C, C, C],
4242
[D, D, C, C, D, D], random_seed=2)
4343

4444

axelrod/tests/unit/test_memoryone.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,7 @@ def test_effect_of_strategy(self):
229229
self.responses_test([C], [C], [D, D, C, C], random_seed=2)
230230
self.responses_test([C], [D], [D, D, C, C], random_seed=2)
231231
self.responses_test([D], [C], [D, D, C, C], random_seed=2)
232-
self.responses_test([D], [D], [D, D, C, C], random_seed=2)
232+
self.responses_test([C], [D], [D, D, C, C], random_seed=2)
233233

234234

235235
class TestZDExtort2v2(TestPlayer):

axelrod/tests/unit/test_random_.py

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,15 @@
88

99
C, D = Actions.C, Actions.D
1010

11+
1112
class TestRandom_(unittest.TestCase):
1213

1314
def test_return_values(self):
1415
self.assertEqual(random_choice(1), C)
1516
self.assertEqual(random_choice(0), D)
16-
random.seed(1)
17+
seed(1)
1718
self.assertEqual(random_choice(), C)
18-
random.seed(2)
19+
seed(2)
1920
self.assertEqual(random_choice(), D)
2021

2122
def test_set_seed(self):
@@ -30,3 +31,14 @@ def test_set_seed(self):
3031

3132
self.assertEqual(numpy_random_numbers[0], numpy_random_numbers[1])
3233
self.assertEqual(stdlib_random_numbers[0], stdlib_random_numbers[1])
34+
35+
def test_seed_not_offset_by_deterministic_call(self):
36+
"""Test that when called with p = 0 or 1, the random seed is not
37+
affected"""
38+
for p in [0, 1]:
39+
seed(0)
40+
r = random.random()
41+
42+
random.seed(0)
43+
random_choice(p)
44+
self.assertEqual(r, random.random())

0 commit comments

Comments
 (0)