I wanted to test how evenly distributed the numpy randint function is, so I did this.
>>> a = np.random.randint(0, 100, 10000)
>>> a.mean()
`49.1685`
>>> a = np.random.randint(0, 100, 1000000)
>>> a.mean()
`49.494202`
>>> a = np.random.randint(0, 100, 1000000000)
>>> a.mean() <br>`49.49944384`
I was confused about why it was reaching 49.49 as an average. I figured someone else out there would have the same question.
Took me a minute to realize.
The range(0, 100)
only includes numbers ranging from 0-99.
The code speaks for itself:
>>> a = np.random.randint(0, 100, 1000000000)
>>> a.mean()
`49.49944384`
>>> a = np.random.randint(0, 101, 1000000000)
>>> a.mean()
`50.000402272`
Because np.random.randint(0, 100)
generates a number between 0 (included) and 100 excluded , ie the max is 99.
So the mean is at (0 + 99) / 2 = 49.5
.
Illustration that upper bound is not included:
np.randint(0, 2, 100) # only 0s and 1s
Side note: this behavior is consistent with various functions and syntax in Python. For instance, with the range
function:
[i for i in range(0, 5)] # [0, 1, 2, 3, 4]
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.