Implementing random algorithms in Python