Python random not working like
numpy, python, random
Solution
The reason for the observed difference is that `random.sample` samples without replacement (see here), while `numpy.random.random_integers` samples with replacement.
Problem
Attempted problem: The probability that one of two dice will have a higher value than a third die. Problem: For some reason, when I use the `random` module from python (specifically the sample method), I end up with a different (and incorrect) result from when when I use numpy. I've included the results at the bottom. Repeated execution of the code yields similar results. Any ideas, why the `random.sample` method and the `numpy.random.random_integers` have different results even though they have the same function? ``` import numpy as np import random random_list = [] numpy_list = [] n= 500 np_wins = 0 rand_wins = 0 for i in range(n): rolls = random.sample(range(1,7), 3) rand_wins += any(rolls[0] < roll for roll in rolls) rolls = np.random.random_integers(1, 6, 3) np_wins += any(rolls[0] < roll for roll in rolls) print "numpy : {}".format(np_wins/(n * 1.0)) print "random : {}".format(rand_wins/(n * 1.0)) ``` Result: ``` Press ENTER or type command to continue numpy : 0.586 random : 0.688 ```