ManimCommunity / manim

A community-maintained Python framework for creating mathematical animations.
https://www.manim.community
MIT License
19.94k stars 1.48k forks source link

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

Open Jaidenmagnan opened 2 weeks ago

Jaidenmagnan commented 2 weeks ago

Reviewer Checklist

JasonGrace2282 commented 2 weeks ago

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?

uwezi commented 2 weeks ago

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.

JasonGrace2282 commented 1 week ago

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?

Jaidenmagnan commented 1 week ago

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: @.***>

JasonGrace2282 commented 1 week ago

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)
Jaidenmagnan commented 1 week ago

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.

JasonGrace2282 commented 1 week ago

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 commented 5 days ago

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