8000 Backport2793 by certik · Pull Request #2820 · numpy/numpy · GitHub
[go: up one dir, main page]

Skip to content

Backport2793 #2820

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Dec 15, 2012
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
BUG: internal call fix in random.choice
An random.random call from within mtrand was done by a call to
np.random.random instead of inside the class. This can possibly lead
to non-deterministic results after seeding.
  • Loading branch information
seberg authored and certik committed Dec 15, 2012
commit eb77c8d75e304da6dd7861a02fd4158db812e8e8
20 changes: 7 additions & 13 deletions numpy/random/mtrand/mtrand.c

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion numpy/random/mtrand/mtrand.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -1026,7 +1026,7 @@ cdef class RandomState:
if None != p:
cdf = p.cumsum()
cdf /= cdf[-1]
uniform_samples = np.random.random(shape)
uniform_samples = self.random_sample(shape)
idx = cdf.searchsorted(uniform_samples, side='right')
idx = np.array(idx, copy=False) # searchsorted returns a scalar
else:
Expand Down
10 changes: 10 additions & 0 deletions numpy/random/tests/test_regression.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,5 +63,15 @@ def test_shuffle_mixed_dimension(self):
random.shuffle(shuffled)
assert_array_equal(shuffled, [t[0], t[3], t[1], t[2]])

def test_call_within_randomstate(self):
# Check that custom RandomState does not call into global state
m = np.random.RandomState()
res = np.array([0, 8, 7, 2, 1, 9, 4, 7, 0, 3])
for i in range(3):
np.random.seed(i)
m.seed(4321)
# If m.state is not honored, the result will change
assert_array_equal(m.choice(10, size=10, p=np.ones(10.)/10), res)

if __name__ == "__main__":
run_module_suite()
0