# how to properly test random functions

**URL:** <https://igraph.discourse.group/t/how-to-properly-test-random-functions/595>\
**Category:** Development\
**Created:** [14 January 2021 12:20 UTC](https://igraph.discourse.group/t/how-to-properly-test-random-functions/595 "2021-01-14T12:20:35Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![GroteGnoom](https://yyz2.discourse-cdn.com/free1/user_avatar/igraph.discourse.group/grotegnoom/32/428_2.png) [@GroteGnoom](https://igraph.discourse.group/u/GroteGnoom)\
**Post date:** [14 January 2021 12:20 UTC](https://igraph.discourse.group/t/how-to-properly-test-random-functions/595/1 "2021-01-14T12:20:35Z")

</div>

@szhorvat  
For example igraph\_growing\_random\_game. Should I only test things that are the same for each call (e.g. the number of vertices) ?

I tried

```auto
#undef RNG_BEGIN
#define RNG_BEGIN() igraph_rng_seed(igraph_rng_default(), 0);	\
	igraph_rng_default()->def=2;}

```

to fix the seed, but that didn’t work out, and I am not sure that’s the right way to go anyway.

---

<div class="post-metadata">

**Author:** ![vtraag](https://yyz2.discourse-cdn.com/free1/user_avatar/igraph.discourse.group/vtraag/32/38_2.png) [@vtraag](https://igraph.discourse.group/u/vtraag)\
**Post date:** [14 January 2021 13:08 UTC](https://igraph.discourse.group/t/how-to-properly-test-random-functions/595/2 "2021-01-14T13:08:43Z")

</div>

You can set the seed using `igraph_rng_seed(igraph_rng_default(), 0)` as you already found out. There is no need to redefine `RNG_BEGIN`. Is there any specific that is not working as expected when setting the seed?

There are quite some other functions that are stochastic, or use a random graph, and hence depend on the rng seed. You could also take a look at those if you want to see how they are dealing with this situation, e.g. `tests/unit/igraph_coloring.c` to name just one example.

---

<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:** [14 January 2021 17:33 UTC](https://igraph.discourse.group/t/how-to-properly-test-random-functions/595/3 "2021-01-14T17:33:21Z")

</div>

Many random functions are not tested, and we don’t have proper guidelines on how to do it. Ideas are welcome. Here are a few from the top of my head:

- Do set a seed explicitly, for example `igraph_rng_seed(igraph_rng_default(), 42)`. (Note: using `0` is likely fine, personally I’m averse to it … with _some_ not very well implemented RNGs, certain seeds have been known to produce suboptimal output)

- Do not use `RNG_BEGIN` in user code (it’s only for internal use to ensure that he default generator was initialized).

- Think about what properties should be the same for all generated instances.

- It is quite tedious, but it is useful to check some edge cases. E.g., what if `n=0`? (I fixed lots of edge-case failures in the past.) It is also useful to have a glance at the source code and make sure that the function does check its input arguments for validity. If it doesn’t, add it to [All graph generators should check their input for validity · Issue #1322 · igraph/igraph · GitHub](https://github.com/igraph/igraph/issues/1322)

- In some cases, even though the function is random, for _some_ input parameters there is only one valid output. One such example is [threshold degree sequences](https://en.wikipedia.org/wiki/Threshold_graph) with `igraph_degree_sequence_game`—there is precisely one simple graph realization of these sequences. It’s useful to add such tests.

- Even if the test is fairly limited, it is still useful, as it exercises the function, and gives AddressSanitizer a chance to find problems. IMO this is the most important reason to add these tests.
