# Get random seed value in p5.js?

**URL:** <https://discourse.processing.org/t/get-random-seed-value-in-p5-js/32635>\
**Category:** Beginners\
**Created:** [October 5, 2021, 1:35pm UTC](https://discourse.processing.org/t/get-random-seed-value-in-p5-js/32635 "2021-10-05T13:35:37Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![jeffthompson](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jeffthompson/32/743_2.png) [@jeffthompson](https://discourse.processing.org/u/jeffthompson)\
**Post date:** [October 5, 2021, 1:35pm UTC](https://discourse.processing.org/t/get-random-seed-value-in-p5-js/32635/1 "2021-10-05T13:35:37Z")

</div>

I’ve looked at the [source code for the p5.js `randomSeed()` function](https://github.com/processing/p5.js/blob/v1.4.0/src/math/random.js#L37) but can’t quite figure this out: is it possible to get the seed value currently being used by the `random()` function? I’d like to grab this is `setup()` so I can replicate the results later by feeding in this seed value.

Thanks!

---

<div class="post-metadata">

**Author:** ![BrendanL](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/brendanl/32/14076_2.png) [@BrendanL](https://discourse.processing.org/u/BrendanL)\
**Post date:** [October 5, 2021, 4:05pm UTC](https://discourse.processing.org/t/get-random-seed-value-in-p5-js/32635/2 "2021-10-05T16:05:07Z")

</div>

Looking through the code I didn’t see an obvious way to get the seed. You could just set it yourself at the beginning of your program to have a repeatable value.

---

<div class="post-metadata">

**Author:** ![jb4x](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jb4x/32/789_2.png) [@jb4x](https://discourse.processing.org/u/jb4x)\
**Post date:** [October 5, 2021, 4:56pm UTC](https://discourse.processing.org/t/get-random-seed-value-in-p5-js/32635/3 "2021-10-05T16:56:03Z")

</div>

I also don’t think that you can. As @BrendanL said, you can always set one yourself that you can reuse.  
Have a look at this post: [randomSeed() reuse - #14 by jb4x](https://discourse.processing.org/t/randomseed-reuse/32154/14)

---

<div class="post-metadata">

**Author:** ![arathorn76](https://avatars.discourse-cdn.com/v4/letter/a/4bbf92/32.png) [@arathorn76](https://discourse.processing.org/u/arathorn76)\
**Post date:** [October 5, 2021, 9:09pm UTC](https://discourse.processing.org/t/get-random-seed-value-in-p5-js/32635/4 "2021-10-05T21:09:41Z")

</div>

I´d say in most scenarios for pseudo random numbers (as generated by random()) finding out the seed value is somewhere between “doesn´t matter” and “definitely not wanted”.

This explains why it is not (easy) to get the current seed.

For cases like yours where knowledge of the seed is interesting/important seeding with a known value is therefore at least the easiest way - if not the only one.

---

<div class="post-metadata">

**Author:** ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)\
**Post date:** [October 5, 2021, 9:53pm UTC](https://discourse.processing.org/t/get-random-seed-value-in-p5-js/32635/5 "2021-10-05T21:53:59Z")

</div>

> [@jeffthompson](#):
>
> Is it possible to get the seed value currently being used by the **random()** function?

Until **randomSeed()** is invoked for the 1st time there’s no seed yet, so **random()** merely invokes JS builtin Math.**random()** till then:

> <https://github.com/processing/p5.js/blob/v1.4.0/src/math/random.js#L129-L133>

The local constant _randomStateProp_ contains the string name of the “secret” seed property:

> <https://github.com/processing/p5.js/blob/v1.4.0/src/math/random.js#L10-L11>

And that property is initialized w/ `null` by default:

> <https://github.com/processing/p5.js/blob/v1.4.0/src/core/main.js#L207-L209>

We can log that property like this: `print(_lcg_random_state);`

AFAIK that “seed” algorithm is used by both Java Mode & Pjs flavors as well.

---

<div class="post-metadata">

**Author:** ![jeffthompson](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jeffthompson/32/743_2.png) [@jeffthompson](https://discourse.processing.org/u/jeffthompson)\
**Post date:** [October 6, 2021, 3:54pm UTC](https://discourse.processing.org/t/get-random-seed-value-in-p5-js/32635/6 "2021-10-06T15:54:56Z")

</div>

Thanks all! I realize this is maybe a but unusual but I could see a good use here: when working with random values you often want variety (ie randomness) but also to be able to return to something you like. By running `random()` with the built-in seed, each time you run the sketch the results are different. But by being able to print that seed (or use it to build a filename for the output) you can return to those exact settings later.

For sure: I could create a seed myself and I do that a lot, but this involves updating that seed manually.

@GoToLoop – thanks for tracking that down! I tried several permutations but `_lcg_random_state` always returns `null` for me.

Seems like maybe the best option is something like this:

```auto
function setup() {
  createCanvas(400, 400);
  
  // create a random random-seed :)
  let seed = random(0, 10000);
  randomSeed(seed);
}

function draw() {
  background(220);
  
  // do random stuff
}

```
