Skip to content

Commit

Permalink
Fix random differences between python 2 and 3
Browse files Browse the repository at this point in the history
  • Loading branch information
marcharper committed Apr 13, 2016
1 parent 992a059 commit 28f2ef4
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 1 deletion.
3 changes: 2 additions & 1 deletion axelrod/moran.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from .deterministic_cache import DeterministicCache
from .match import Match
from .player import Player
from .random_ import randrange


def fitness_proportionate_selection(scores):
Expand Down Expand Up @@ -58,7 +59,7 @@ def __next__(self):
# Fitness proportionate selection
j = fitness_proportionate_selection(scores)
# Randomly remove a strategy
i = random.randrange(0, len(self.players))
i = randrange(0, len(self.players))
# Replace player i with clone of player j
self.players[i] = self.players[j].clone()
self.populations.append(self.population_distribution())
Expand Down
6 changes: 6 additions & 0 deletions axelrod/random_.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,9 @@ def random_choice(p=0.5):
if r < p:
return Actions.C
return Actions.D

def randrange(a, b):
"""Python 2 / 3 compatible randrange."""
c = b - a
r = c * random.random()
return a + int(r)

0 comments on commit 28f2ef4

Please sign in to comment.