mtrand fixes backport for 1.8

backport for issues gh-3173, gh-3263 and gh-4270