Removed the dependency on math/rand and slightly simplified the code.