Note

This documents the development version of NetworkX. Documentation for the current release can be found here.

networkx.utils.decorators.preserve_random_state

preserve_random_state(func)[source]

Decorator to preserve the numpy.random state during a function.

Parameters
funcfunction

function around which to preserve the random state.

Returns
wrapperfunction

Function which wraps the input function by saving the state before calling the function and restoring the function afterward.

Notes

If numpy.random is not importable, the state is not saved or restored.

Examples

Decorate functions like this:

@preserve_random_state
def do_random_stuff(x, y):
    return x + y * numpy.random.random()