Random numbers in Javascript

due_friday's icon

I am trying to get a patch working that I export to Javascript. However it uses random processes and I noticed the sound being the same on every refresh of the website. Creating a simple RNBO patch that outputs a random number to the console when given any input confirms my suspicion: it seems that the seed during compilation is fixed, i.e. once compiled, the sequence of random numbers is the same after refreshing the webpage (RNBO v1.2.4).

Max Patch
Copy patch and select New From Clipboard in Max.

In order to get a different sequence on every refresh, I need to send a seed generated by Javascript manually before starting.

I am not sure if this intended behavior. I could understand if it has to be this way, but the behavior then wildly different from running the RNBO patch originally within the Max environment. Also, the help files are also very misleading in that case:

Thanks!

due_friday's icon

What's worse, every random object has its own random generator. Sending a seed to one random object does not update the others and they continue to spit out the same sequence of "random" numbers after every webpage refresh. So, I am required to send a separate seed to all of them individually. If I simply send the one seed I generated in Javascript to all of them, they all produce the exact same sequence. So, my patch complexity just grew immensely, managing the distribution of random seeds all over the place. I believe I need to ask if this can be fixed in the next update.

Let me know if I am missing something.

Thanks

due_friday's icon

If I am not mistaken, whichever "seed" is used in the compiled version of the patch for Javascript seems to be tied to the number of random objects contained in the RNBO patch.

At first I was getting different results on every compile, because I was testing things. But then I noticed that sometimes, after compiling, I would get the same result (i.e. the random objects behaving identically). I believe that I provoke a new result ("seed") only if I change the number of random objects in the RNBO patch.

Aidan's icon

Did you find a solution to this? running into the same problem now. Specifically interested in how you managed to manipulate the seed inside the json from the javascript shell

due_friday's icon

Hi, not a solution, but a workaround:

For every [random] object, I placed a subpatcher before it that transforms an incoming number with hard-coded, but random scalars:

Max Patch
Copy patch and select New From Clipboard in Max.

So, you create a dedicated input to your RNBO patcher which accepts a single seed value. This seed you will generate every time the webpage loads, randomly, on the javascript side, and send it into the RNBO patch. On the Max/RNBO side, you accept this seed and send it to all [random] objects via a unique [send]/[receive] identifier (see subpatcher in my example code). In every instance, you modify this original seed value uniquely (I multiply it and then subdivide it by two unique scalars for every [random] object), which creates a unique seed for every [random] object.

Aidan's icon

Thank you! Yeah I'm not sure honestly why random would export to wasm without some seed defined by default anyways, but glad you were able to find this workaround until c74 patches this (hopefully!)