processing / p5.js

p5.js is a client-side JS platform that empowers artists, designers, students, and anyone to learn to code and express themselves creatively on the web. It is based on the core principles of Processing. http://twitter.com/p5xjs —
http://p5js.org/
GNU Lesser General Public License v2.1
21.52k stars 3.3k forks source link

Feature proposal - calling randomSeed() and noiseSeed() without arguments should return the seed #2606

Closed cdfuller closed 6 years ago

cdfuller commented 6 years ago

Hi all. Right now there is no way for sketches that rely on random numbers to be reproduced without seeding. I'd like to be able get the seed used for random() and noise() without having to set it beforehand and manually change it to see different results. Please let me know if this use case is not clear.

Following the contribution guidelines, I'd love to discuss this, get the greenlight, and then implement it.

One concern I have is that right now in random() we only use the Linear Congruential Generator when random has been seeded, otherwise we use Math.random(). We would have to change to always using the LCG because control of seeding Math.random() is left up to the browser. Would that introduce speed issues?

lmccart commented 6 years ago

I'm not sure I'm clear on the use case. Why not just set the seed for the initial sketch and manually change to see different results?

cdfuller commented 6 years ago

That's what I was hoping to avoid. I feel it slows you down when you have to switch between editor and browser every time you want to get a new result.

It feels intuitive to me for randomSeed() and noiseSeed() to have the same type of functionality as frameRate(). Where if you pass a number, it sets it, otherwise it returns the value.

Thinking through it, I've realized that I could pick a random number using Math.random(), set it to a global, and then pass it to noiseSeed() and randomSeed(). This would also accomplish the same functionality on a per-sketch basis.

If you think this would be a feature that others would use, I'd be more than happy to implement it. Otherwise, feel free to close the issue.

Have a great day!

limzykenneth commented 6 years ago

I suppose it probably isn't too hard to implement but I guess the aim here is to identify a concrete use case for this functionality, just so that we don't add functionalities that are extraneous.

From what I can think of, for randomSeed() to return the seed to be useful it may be the case that you are saving metadata about the sketch to a server so it can be playedback as is later or passed to a different sketch. However, that feels like defeating the purpose of random and also as @cdfuller mentioned, saving the seed passed to randomSeed() when running the sketch can also do the same thing without too much overhead.

If we didn't miss any potential use case I think we can close this for now.

RobinLefebvre commented 6 years ago

Using noise and randomness for procedural generation is very useful, specifically because when seeded the results are reliably identical. For my current project, I am attempting to generate various levels of details for 2D maps.

The use of a seeded noise allows me to store only a given set of data into a JSON to generate the same mesh on the fly, without needing to store the map at all (which, given the size I'm going for, is not an option). The only thing I need is the ability to store the seed that I'm using while at the same time allowing a random seed to be used.

The fact is, when dealing with this kind of procedural generation, the ability to store seed values is important. As was said, there are workarounds, but I think that it'd be a non-useless feature for those two functions to return a value even when the random number generator is "seeded randomly", in other words not seeded at all.

limzykenneth commented 6 years ago

@RobinLefebvre I guess the point we had is that in the following two cases:

instead of

seedRandom(random());

// Procedural drawings

you can just do

var seed = random();
seedRandom(seed);

// Procedural drawings

// Save the variable "seed" out

As for the case when you need to find out a seed to an unseeded RNG, I'm not really sure there is a way to do that natively (browser Math.random() which random() uses under the hood don't provide that I think, unless we forgo that and use custom seeded RNG all the time) and in this case since you will have already anticipated that you will want to retrieve the seed, you can easily add the two lines in the second example for the same effect.