# set up seed for community\_leiden

**URL:** <https://igraph.discourse.group/t/set-up-seed-for-community-leiden/2193>\
**Category:** Usage\
**Tags:** Python\
**Created:** [10 August 2025 00:04 UTC](https://igraph.discourse.group/t/set-up-seed-for-community-leiden/2193 "2025-08-10T00:04:17Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![JellyZhangxc](https://yyz2.discourse-cdn.com/free1/user_avatar/igraph.discourse.group/jellyzhangxc/32/1220_2.png) [@JellyZhangxc](https://igraph.discourse.group/u/JellyZhangxc)\
**Post date:** [10 August 2025 00:04 UTC](https://igraph.discourse.group/t/set-up-seed-for-community-leiden/2193/1 "2025-08-10T00:04:17Z")

</div>

Hi, there  
is there any way we could set up a seed for community\_leiden? thanks

seems kwds does not support any other argument except resolution\_parameter

 ![image](https://global.discourse-cdn.com/free1/uploads/igraph/original/2X/7/7f160de0405aa76781769d0d9c738764d559f3fc.png)

---

<div class="post-metadata">

**Author:** ![szhorvat](https://yyz2.discourse-cdn.com/free1/user_avatar/igraph.discourse.group/szhorvat/32/3_2.png) [@szhorvat](https://igraph.discourse.group/u/szhorvat)\
**Post date:** [10 August 2025 12:04 UTC](https://igraph.discourse.group/t/set-up-seed-for-community-leiden/2193/2 "2025-08-10T12:04:34Z")

</div>

igraph uses Python’s built-in `random` module for all random number generation. You can simply set a seed using `random.seed()` directly before the `community_leiden()` call.

If you don’t want to affect any other functions by setting a seed, you can save and restore the random state. I’m not very good with Python, but here’s something I cobbled together with some AI help using [contextmanager](https://docs.python.org/3/library/contextlib.html#contextlib.contextmanager):

```python
from contextlib import contextmanager
import random

@contextmanager
def local_random(seed=None):
    """Temporarily set the random state, restoring it afterwards."""
    state = random.getstate()
    if seed is not None:
        random.seed(seed)
    try:
        yield
    finally:
        random.setstate(state)

```

This allows you to run a function in a local random context, without affecting the random state, and optionally seed that local context. You will need to use

```python
with local_random(seed=123):
    g.community_leiden()

```

* * *

@tamas Is there anything similar to this that is built into Python? If not, why don’t we provide something similar to this `local_random` as part of python-igraph? It would satisfy a common demand.

BTW this is modelled on Mathematica’s [`BlockRandom`](https://reference.wolfram.com/language/ref/BlockRandom.html), which I use frequently.

---

<div class="post-metadata">

**Author:** ![tamas](https://yyz2.discourse-cdn.com/free1/user_avatar/igraph.discourse.group/tamas/32/1261_2.png) [@tamas](https://igraph.discourse.group/u/tamas)\
**Post date:** [10 August 2025 16:32 UTC](https://igraph.discourse.group/t/set-up-seed-for-community-leiden/2193/3 "2025-08-10T16:32:44Z")

</div>

I couldn’t believe that there’s nothing like this built into Python’s `random` module or NumPy but apparently there isn’t. It’s weird, but probably there are reasons for that:

1. it does not play nicely with Python’s `async` functions where you could in theory have two or more of these context managers running in parallel such that they exit not exactly in the reverse order.
2. since you are manipulating the state of a global RNG, you still need to ensure that you do not have any other threads working with this RNG to get reproducible results

I think that if you need reproducible results from igraph, a better approach would be to set igraph’s RNG to a local instance with an interface identical to the `random`module, but I don’t mind adding such a function to igraph itself. It’s just that it really has nothing to do with igraph - it really belongs to Python’s standard library, either in the `contextlib` module or in the `random` module.

---

<div class="post-metadata">

**Author:** ![szhorvat](https://yyz2.discourse-cdn.com/free1/user_avatar/igraph.discourse.group/szhorvat/32/3_2.png) [@szhorvat](https://igraph.discourse.group/u/szhorvat)\
**Post date:** [10 August 2025 16:45 UTC](https://igraph.discourse.group/t/set-up-seed-for-community-leiden/2193/4 "2025-08-10T16:45:21Z")

</div>

We have two reasonable options:

1. Add it to python-igraph.
2. Add to the community detection tutorial that @Bea_Marton is writing, to show people how to do it.

What do you prefer?

---

<div class="post-metadata">

**Author:** ![tamas](https://yyz2.discourse-cdn.com/free1/user_avatar/igraph.discourse.group/tamas/32/1261_2.png) [@tamas](https://igraph.discourse.group/u/tamas)\
**Post date:** [11 August 2025 21:43 UTC](https://igraph.discourse.group/t/set-up-seed-for-community-leiden/2193/5 "2025-08-11T21:43:28Z")

</div>

Just add it to the community detection tutorial. Thanks!

---

<div class="post-metadata">

**Author:** ![szhorvat](https://yyz2.discourse-cdn.com/free1/user_avatar/igraph.discourse.group/szhorvat/32/3_2.png) [@szhorvat](https://igraph.discourse.group/u/szhorvat)\
**Post date:** [11 August 2025 22:57 UTC](https://igraph.discourse.group/t/set-up-seed-for-community-leiden/2193/6 "2025-08-11T22:57:35Z")

</div>

@Bea_Marton, can you please incorporate it, and use it in the example that shows the stochasticity of some methods?
