-
-
Notifications
You must be signed in to change notification settings - Fork 3.5k
[2.x] Add a way to get the value of the current random seed #7711
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
Welcome! 👋 Thanks for opening your first issue here! And to ensure the community is able to respond to your issue, please make sure to fill out the inputs in the issue forms. Thank you! |
For simplicity and consistency, how about using the same model as So calling The same could be applied to |
That was my initial idea too. However, according to the source code, calling
This is useful if you want to simply randomize the seed and don't care about setting it to a specific value. But that whould, presumably, break backwards compatibility for projects that rely on the current behavior of |
Ah good catch! Thanks for looking into this. I don't think my consistency/simplicity argument supports breaking backwards compatibility, so anyone looking at this feel free to ignore it.
|
Great point! @SableRaf @XenusYard what do you think about Another idea would be to have Since 2.0 release is around the corner, I would suggest we keep this as a possible addition in 2.x post-release. This is because the proposed timeline is to not expand 1.x once 2.0 is released, even though 1.x is still available for a while (info: #7488). Do you mind adding that to issue body/title please? Other relevant API precedent to consider (source: https://beta.p5js.org/reference/): p5.js 2.0 typography uses 1 function as both getter and setter (though with And |
Thanks @ksen0
I like this approach. Adding I don't know what the consequence would be on older sketches that used the same variable names but I believe this type of name collision is already handled by the FES. |
That would be ideal, but wouldn't that create a name conflict? How would you distinguish between the function and the variable? Or are you talking about literal ES6 getters and setters?
But what if you want to get the current seed value without changing it? If you set the seed first, then return the value — you've lost the previous seed value, the one you were trying to get in the first place. If you save the previous seed value before randomizing and return that instead — you can never get the new value of the seed before changing it first. I believe the first solution you proposed would be better.
Yeah, sure! Should I just prepend the title with "[2.x]"? |
Just to mentioned that this has been looked into before in #2606 and to rehash the point in that old issue, while this is possible to implement, I'm not 100% sure what the use case is. It is possible to save the current seed including a random one with this snippet: var seed = random();
randomSeed(seed); Although it feels cumbersome, as mentioned in the issue post, I don't really see another way around it, ie. at the minimum we still have to do randomSeed(random());
// Later
var seed = randomSeed(); So just based on this I still don't fully see the use case, unless there is something else I missed? For current behavior of |
That's not really true, since p5 randomizes the seed by default. You don't need to call There are two main aspects to my argument: convenience and consistency. The original drive for this proposal comes from my own experience and aligns with the convenience aspect. There have been several times when I was working on a project that was centered around randomness and encountered a bug that was only apparent in a certain configuration of the system. And since the entire system is based around randomness it is very difficult in those kinds of situations to reproduce a specific state, which makes debugging rather unpleasant and inefficient. Now, is it my fault for being imprudent and not preparing for these situations in advance? Yes, absolutely. There is no reason for me not to save the random seed value ahead of time, other then it being, well, inconvenient. It feels like something that should be taken care of by the framework. It stores the seed somewhere in its state. Why not let the developer easily fetch it at any time, without having to worry about it beforehand? It makes developing and prototyping more cumbersome and verbose than it needs to be. When it comes to consistency, it seems odd to me that random seed doesn't get the same treatment like other state variables that are part of p5: width and height of the canvas, frame rate, frame count etc. After all, we don't have to save the value of the target frame rate in a variable every time we want to have access to it — we simply call |
That is not possible, when
Sorry to kinda repeat here as this is the part I don't really understand, you will have to set the random seed ahead of time, as per above, and so that value must have existed before The reason this is potentially treated differently is back to the first point above, the RNG when not seeded is using the more performant |
Ah. See, I was not aware of that. Yeah, I will have to concede that kind of undermines the entire convenience point, unfortunately. So, with that correction in mind, the discussion comes down to which of the following paradigms is the most sensible and whether the pros of a new feature outweigh the cons of introducing it: (NOTE: the following applies equally to 1) Leave as is:// Initializing
let seed = random();
randomSeed(seed);
// Retrieving
print(seed); Pros:
Cons:
2) Introduce getter variables:// Initializing
randomSeed();
// Retrieving
print(randomSeed); Pros:
Cons:
3) Change the return value of
|
Increasing access
I believe this feature would increase the consistency of the API (i.e. you can both set and get the value of the current frame rate, for instance, but you can only set and never get the value of the random seed) and make iterative development centered around randomness easier and friendlier.
Most appropriate sub-area of p5.js?
Feature request details
It is sometimes useful to know the value of the random seed that produced a specific result — be it for debugging or presentation purposes, or something else.
Currently the only way to know the seed value in advance is to generate it with
random()
first, save that value and pass it torandomSeed()
. It is cumbersome and requires setting it up every time you want to keep track of your random seeds.Without a similar setup reproducing a specific result may be a tedious and unreliable process — especially if the degree of accuracy required is very strict.
Thus, I propose adding a way of fetching the current value of the random seed, perhaps in a form of a variable (e.g.
seed
) or a function (e.g.getRandomSeed()
orseed()
). That way the value could be conveniently retrieved at any moment during runtime (for example, from the dev console in the browser) and referenced later.The text was updated successfully, but these errors were encountered: