I think the example works more quickly than it should due to the line "if(ff.clock>1) ff.clock=1;" Every time the clock goes over 1, wouldn't you mod the clock value, not set it to 1 exactly?
No, partly because fireflies take a certain amount of time to "charge" so jumping mid cycle might not be plausible physically.
So what this line does (which is in the section of code where a flash alters the fireflies around it) is that after advancing the other fireflies clocks, if any are over 1 then it puts them in the right state to flash on the next time they're evaluated. If you mod it, then you would have the logic:
"if I'm a firefly and see my neighbour flash while just about to flashing myself, don't flash but reset my clock to part way through the cycle"
rather than
"if I'm a firefly and see my neighbour flash while just about to flashing myself, flash as well"
Edit - setting it to one ensures that each neighbour can't keep adding to the clock, advancing it to a random point in the cycle.
If the fireflies take that time in their clocks to "charge the light", could it be that when they nudge their clock once or more in the cycle, the intensity of the light is a bit lower than it would normally be?
Interesting question. I honestly thought that the process was actually to reset the clocks not move them forwards, that's how I've simulated it in the past.
I think that's why it works -- if you change that = to a %=, the effect no longer happens.
It does make (intuitive) sense, though, because the only time that makes a significant difference is when a lot of fireflies flash around one firefly simultaneously, in which case the firefly wouldn't decide to flash twice in a row but instead interpret it as a singular flash. And if a firefly sees another one flash when it's about to, it would naturally want to synchronize its flashing with the other.
Right- but the explanation in the 'explorable' is that the effect happens due to the simple pattern of Step 1: When you see a nearby firefly flash, nudge your clock a little bit forward, Step 2: That's it. Problem is, the description of what's happening and what is actually happening doesn't match.
Another poster here showed their own, similar demo of this. There they had each firefly charging like a capacitor. Triggering the light causes the capacitor to deplete. This seems like the better analogy, but more people know what clocks are.
In addition to being a better analogy, I am told it is possible to replicate the effect with a physical capacitor and a gas-discharge lamp: https://news.ycombinator.com/item?id=4600996