<br />

|---|
| [![](https://www.tensorflow.org/images/GitHub-Mark-32px.png) View source on GitHub](https://github.com/tensorflow/tensorflow/blob/v2.21.0/tensorflow/python/ops/stateful_random_ops.py#L1000-L1031) |

Replaces the global generator with another `Generator` object.

#### View aliases


**Main aliases**

[`tf.random.experimental.set_global_generator`](https://www.tensorflow.org/api_docs/python/tf/random/set_global_generator)
**Compat aliases for migration**

See
[Migration guide](https://www.tensorflow.org/guide/migrate) for
more details.

[`tf.compat.v1.random.experimental.set_global_generator`](https://www.tensorflow.org/api_docs/python/tf/random/set_global_generator), [`tf.compat.v1.random.set_global_generator`](https://www.tensorflow.org/api_docs/python/tf/random/set_global_generator)

<br />

    tf.random.set_global_generator(
        generator
    )

This function replaces the global generator with the provided `generator`
object.
A random number generator utilizes a [`tf.Variable`](https://www.tensorflow.org/api_docs/python/tf/Variable) object to store its state.
The user shall be aware of caveats how `set_global_generator` interacts with
[`tf.function`](https://www.tensorflow.org/api_docs/python/tf/function):

- tf.function puts restrictions on Variable creation thus one cannot freely create a new random generator instance inside [`tf.function`](https://www.tensorflow.org/api_docs/python/tf/function). To call `set_global_generator` inside [`tf.function`](https://www.tensorflow.org/api_docs/python/tf/function), the generator instance must have already been created eagerly.
- tf.function captures the Variable during trace-compilation, thus a compiled f.function will not be affected `set_global_generator` as demonstrated by random_test.py/RandomTest.testResetGlobalGeneratorBadWithDefun .

For most use cases, avoid calling `set_global_generator` after program
initialization, and prefer to reset the state of the existing global generator
instead, such as,

    rng = tf.random.get_global_generator()
    rng.reset_from_seed(30)

<br />

<br />

<br />

| ## Args ||
|---|---|
| `generator` | the new `Generator` object. |

<br />