scientific-python / blog.scientific-python.org

Community blog posts on scientific-python.org
https://blog.scientific-python.org
Other
23 stars 33 forks source link

Question about Numpy random numbers blog post #176

Closed thomasjpfan closed 7 months ago

thomasjpfan commented 8 months ago

When using the random state for parallel processing, do we need the private API?

https://github.com/scientific-python/blog.scientific-python.org/blob/b51e4b9df0ea5fd740b0f04c43ab432388deed21/content/posts/numpy/numpy-rng/index.md?plain=1#L108

From my understanding, we can use the public API:

import numpy as np
from joblib import Parallel, delayed

def stochastic_function(high=10, rng=None):
    if not isinstance(rng, np.random.Generator):
        rng = np.random.default_rng(rng)
    return rng.integers(high, size=5)

seed = 319929794527176038403653493598663843656
rng = np.random.default_rng(seed)
child_states = rng.spawn(5)

random_vector = Parallel(n_jobs=2)(
    delayed(stochastic_function)(rng=random_state) for random_state in child_states
)
print(random_vector)

random_vector = Parallel(n_jobs=2)(
    delayed(stochastic_function)(rng=random_state) for random_state in child_states
)
print(random_vector)

CC @albertcthomas

stefanv commented 8 months ago

xref: https://github.com/scientific-python/blog.scientific-python.org/pull/135#discussion_r1470002426

albertcthomas commented 8 months ago

Yes thanks @thomasjpfan ! I will do the PR today :)