manim icon indicating copy to clipboard operation
manim copied to clipboard

Added a seed function to random_color() and random_bright_color()

Open Jaidenmagnan opened this issue 1 year ago • 8 comments

Reviewer Checklist

  • [ ] The PR title is descriptive enough for the changelog, and the PR is labeled correctly
  • [ ] If applicable: newly added non-private functions and classes have a docstring including a short summary and a PARAMETERS section
  • [ ] If applicable: newly added functions and classes are tested

Jaidenmagnan avatar Jun 20 '24 22:06 Jaidenmagnan

Hey, thanks for taking an interest in contributing to Manim! Out of curiosity, what is the use case for this? It seems to me if you want a certain seed you can set it outside of Manim, e.g.

random.seed(...)
rand_color = random_bright_color()

Do you mind elaborating?

JasonGrace2282 avatar Jun 21 '24 13:06 JasonGrace2282

you can set it outside of Manim, e.g.

random.seed(...)
rand_color = random_bright_color()

Do you mind elaborating?

I believe for that to work you would need to know which random package Manim uses internally, numpy.random, random.random or something completely different. And if the randomizer in Manim is contained in its own object, seeding a random random number generator will most likely not affect Manim's random numbers.

The meaning of a seed would be to be able to recreate the same (not so random) random sequence of colors for every subsequent rendering of the scene.

Personally I don't like either of Manim's random color pickers, because the regular one also contains black (or any other background color) and the "light" colors have too little color contrast. But I can understand the notion of having a seed.

uwezi avatar Jun 21 '24 14:06 uwezi

I believe for that to work you would need to know which random package Manim uses internally, numpy.random, random.random or something completely different. And if the randomizer in Manim is contained in its own object, seeding a random random number generator will most likely not affect Manim's random numbers.

Hmm, in that case it might be better to have the seed as a config option then? It seems to me to be a cleaner approach than a seed parameter in every function that uses numpy.random or random. What do you think?

JasonGrace2282 avatar Jun 23 '24 19:06 JasonGrace2282

The config parameter would work well, except you would need a way for subsequent calls to the random function to be reproducible.

On Sun, Jun 23, 2024 at 3:25 PM adeshpande @.***> wrote:

I believe for that to work you would need to know which random package Manim uses internally, numpy.random, random.random or something completely different. And if the randomizer in Manim is contained in its own object, seeding a random random number generator will most likely not affect Manim's random numbers.

Hmm, in that case it might be better to have the seed as a config parameter maybe? That might be a cleaner approach than a seed parameter in every function that uses numpy.random or random. What do you think?

— Reply to this email directly, view it on GitHub https://github.com/ManimCommunity/manim/pull/3813#issuecomment-2185277641, or unsubscribe https://github.com/notifications/unsubscribe-auth/AUIBY7V6UJ44Y46UAJVSGHLZI4OKPAVCNFSM6AAAAABJU2AGOGVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDCOBVGI3TONRUGE . You are receiving this because you authored the thread.Message ID: @.***>

Jaidenmagnan avatar Jun 23 '24 19:06 Jaidenmagnan

except you would need a way for subsequent calls to the random function to be reproducible.

I'm not sure what you mean by this, could you please clarify? In my mind I'm thinking that setting config.seed should just run

np.random.seed(seed)
random.seed(seed)

JasonGrace2282 avatar Jun 23 '24 23:06 JasonGrace2282

For example if you called random_color() with a seed of 5, would you want the function to continuously return red. Or would you want it to return red, blue, yellow, green in the same order.

Jaidenmagnan avatar Jun 24 '24 00:06 Jaidenmagnan

For example if you called random_color() with a seed of 5, would you want the function to continuously return red. Or would you want it to return red, blue, yellow, green in the same order.

Isn't that what setting the seed does? My suggestion is to create a configuration option that sets the seed for numpy.random and random automatically.

JasonGrace2282 avatar Jun 24 '24 00:06 JasonGrace2282

I just realized this parameter is included in the Scene itself (see the signature in the docs, although it could be documented better).

This can be used as such

class MyAnimation(Scene):
    def __init__(self) -> None:
        super().__init__(random_seed=MY_CUSTOM_SEED)

    def construct(self):
        # usual stuff

JasonGrace2282 avatar Jun 30 '24 16:06 JasonGrace2282

Closing as this solution is not a nice way of working with seeds. A better solution could allow something like

class Scene:
    seed: int | None = None

    def __init__(self, ...):
        ...
        if self.seed is not None:
            random.seed(self.seed)
            np.random.seed(self.seed)

But it would be better to create a new PR for that.

JasonGrace2282 avatar Jul 14 '24 01:07 JasonGrace2282