# Deterministic Random Sampling

**URL:** <https://forum.scenic-lang.org/t/deterministic-random-sampling/25>\
**Category:** Ideas\
**Created:** [June 11, 2024, 7:46pm UTC](https://forum.scenic-lang.org/t/deterministic-random-sampling/25 "2024-06-11T19:46:14Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![abel](https://forum.scenic-lang.org/user_avatar/forum.scenic-lang.org/abel/32/31_2.png) [@abel](https://forum.scenic-lang.org/u/abel)\
**Post date:** [June 11, 2024, 7:46pm UTC](https://forum.scenic-lang.org/t/deterministic-random-sampling/25/1 "2024-06-11T19:46:14Z")

</div>

I suggested this a while ago, not sure whether it’s implemented already. Basically, we want the sequence of random samples to be repeatable using a seed. This is important for reproducibility/repeatability of experiments.

This should be possible at least for the built-in NewtonianSimulator. I expect this to be possible in CARLA as well as long as the user-defined agents are deterministic, since CARLA’s traffic manager can be configured using the `carla.TrafficManager.set_random_device_seed()`.

---

<div class="post-metadata">

**Author:** ![dfremont](https://forum.scenic-lang.org/user_avatar/forum.scenic-lang.org/dfremont/32/3_2.png) [@dfremont](https://forum.scenic-lang.org/u/dfremont)\
**Post date:** [June 11, 2024, 8:29pm UTC](https://forum.scenic-lang.org/t/deterministic-random-sampling/25/2 "2024-06-11T20:29:55Z")

</div>

Hi Abel, I can’t recall our previous discussion offhand, but is the [`--seed` option](https://scenic-lang.readthedocs.io/en/latest/options.html#cmdoption-s) not working for you? It is supposed to make Scenic’s own sampling deterministic. Of course, as you point out, if the simulator is nondeterministic there’s not much we can do in general, but calling that CARLA API sounds like a good idea (feel free to make a feature request).

For the specific use case of replying experiments, Scenic also provides [APIs to save and replay simulations](https://scenic-lang.readthedocs.io/en/latest/api.html#storing-scenes-simulations-for-later-use); these have the same caveat about simulators as `--seed`.

---

<div class="post-metadata">

**Author:** ![abel](https://forum.scenic-lang.org/user_avatar/forum.scenic-lang.org/abel/32/31_2.png) [@abel](https://forum.scenic-lang.org/u/abel)\
**Post date:** [June 11, 2024, 10:01pm UTC](https://forum.scenic-lang.org/t/deterministic-random-sampling/25/3 "2024-06-11T22:01:40Z")

</div>

That conversation was in January with Edward I believe. Is the seed option also available when using the Python API?

---

<div class="post-metadata">

**Author:** ![dfremont](https://forum.scenic-lang.org/user_avatar/forum.scenic-lang.org/dfremont/32/3_2.png) [@dfremont](https://forum.scenic-lang.org/u/dfremont)\
**Post date:** [June 11, 2024, 10:04pm UTC](https://forum.scenic-lang.org/t/deterministic-random-sampling/25/4 "2024-06-11T22:04:30Z")

</div>

All the `--seed` option does is call `random.seed()` and `numpy.random.seed()` (see the documentation linked to above), so you can just call those yourself.

---

<div class="post-metadata">

**Author:** ![abel](https://forum.scenic-lang.org/user_avatar/forum.scenic-lang.org/abel/32/31_2.png) [@abel](https://forum.scenic-lang.org/u/abel)\
**Post date:** [June 11, 2024, 10:19pm UTC](https://forum.scenic-lang.org/t/deterministic-random-sampling/25/5 "2024-06-11T22:19:04Z")

</div>

Oh, I see. I guess Edward mentioned it too, I forgot. Thank you!
