hype / HYPE_Processing

HYPE for Processing
BSD 3-Clause "New" or "Revised" License
926 stars 149 forks source link

how does getColor(int seed) work? #165

Closed choiway closed 5 years ago

choiway commented 5 years ago

How does the seed work in getColor(int seed)? I'm sure whether it affects the index from which a color is picked or whether it affect the number of times a color is picked.

tracerstar commented 5 years ago

It's been a good while since I looked at this class and this method, but if memory serves, it allows you to define a seed int in your sketch so that each time you run the sketch you would get the same sequence of random colors. Let me see if I can knock a quick example together.

echophon commented 5 years ago

Yup, seeding is a way of controlling randomness. Most random functions are actually pseudo-random and quietly seeded by something like timestamp. Supplying a seed value is helpful if you want to reproduce a random result, ie you could run your application multiple times and get the same color picks. Here's the relevant bit from HColorPool.

    public int getColor() {
        if(colorList.size() <= 0) return 0;

        int index = (int) Math.floor(H.app().random(colorList.size()));
        return colorList.get(index);
    }

    public int getColor(int seed) {
        HMath.tempSeed(seed);
        int clr = getColor();
        HMath.removeTempSeed();
        return clr;
    }
choiway commented 5 years ago

Thanks for the insight, gents!

tracerstar commented 5 years ago

@choiway this example shows how seed works: https://github.com/hype/HYPE_Processing/blob/lib_staging/examples/HColorPool/HColorPool_004/HColorPool_004.pde

line 33 getColor(i3) play with that a bit and you'll see how it works. Take out the i3 and it's a random color every sketch load. Put it back in, you'll get the same color way each time.