# Can I have multiple independent random number generators with different seeds?

**URL:** <https://discourse.processing.org/t/can-i-have-multiple-independent-random-number-generators-with-different-seeds/10125>\
**Category:** Coding Questions\
**Created:** [April 9, 2019, 11:54am UTC](https://discourse.processing.org/t/can-i-have-multiple-independent-random-number-generators-with-different-seeds/10125 "2019-04-09T11:54:03Z")\
**Posts on this page:** 1\
**Showing post:** 2

<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:** [April 9, 2019, 6:10pm UTC](https://discourse.processing.org/t/can-i-have-multiple-independent-random-number-generators-with-different-seeds/10125/2 "2019-04-09T18:10:26Z")

</div>

> [@neel](#):
>
> Is this possible with p5.js?

Normally, we would expect that each instance of p5 would have its own random seed. 🌱

In such case, we could instantiate a `new` p5 and call its [**noCanvas()**](http://p5js.org/reference/#/p5/noCanvas), just for the sake of using its [**random()**](http://p5js.org/reference/#/p5/random) method w/ a specific [**randomSeed()**](http://p5js.org/reference/#/p5/randomSeed). 🤑

However, the code responsible to generate seeded pseudo-random values in p5js is a singleton object stored in a closure variable named _lcg_: 😐

> <https://github.com/processing/p5.js/blob/0.8.0/src/math/random.js#L16-L46>

B/c _lcg_ is a closure variable, no matter how many times we instantiate the class p5, there’ll only be 1 unique _lcg_ object shared across all p5 instances! 😲

Instead, _lcg_ needs to be changed into a class property, so each instance of that class would have its own _lcg_, rather than a shared 1. 🧐

BtW, the p5js library is littered w/ such closure local variables, which are a dumb, poor, short-sighted attempt to make private, unaccessible states. 🕵️‍♂️

And considering the library is supposed to have multiple instances of it at the same time, it shouldn’t have any shared states at all! 🙅‍♂️

Now, as a workaround, you can simply rewrite the “random.js” file as a class, whose instances will have their own seed state. 💡

More discussion about pseudo-random generation: 🕶

> [@How would I apply randomSeed( )](https://discourse.processing.org/t/how-would-i-apply-randomseed/10054):
>
> Page 221 - 223 - Example 8-9: Move Shapes Randomly At the end of the example, there’s a NOTE about randomSeed( ). I checked out the Reference at [https://p5js.org/reference/#/p5/randomSeed](https://p5js.org/reference/#/p5/randomSeed) I’m trying to figure out how to apply randomSeed( ) to Example 8-9: Move Shapes Randomly? I added randomSeed( ) to example (including the example with constrain( )) and can’t get to work in a way that seems appropriate. Code example without constrain( ): var speed = 2.5; var diameter = 20; var x; var y; fu…

---

_[View the full topic](https://discourse.processing.org/t/can-i-have-multiple-independent-random-number-generators-with-different-seeds/10125)._
