# Algorithm for Randomized Weather

I'm having a hard time modeling weather. Modeling a single aspect is trivial - some sinusoidal pattern + random noise. However more than 1 is blowing my mind. I cannot have wind, falling leaves, rain, and snow all to be random. Simple collisions like rain and snow should never happen.

On top of this I'd like an "intensity" level for maybe each. But this can come after the main logic.

Weather is stored in MySQL.

All your suggestions are welcome, but please don't suggest pinging some zipcode and pulling weather from there.

Utilizing suggestions from, actually, all answers below I've come up with this not-to-scale distribution:

Each feature will be computed with a random noise, and the combination of the 3 will produce overlay effects.

(temp < 0) and (humid > 65) => snow

• By the way, snow+rain is a valid weather. Dec 5 '11 at 7:54
• Well, it won't be in my game. :-D Dec 6 '11 at 16:52
• Isn't snow just the result of rain + low temperature? Sep 27 '14 at 11:50

What about splitting the the weather into more detailed parts?

Precipitation - none/light/moderate/heavy
Wind - none/light/moderate/heavy
Type - rain/hail/snow


You can still store this in a single SQL column if you want by making it a simple enum with 48 (4*4*3) possible values (removing any strange conflicts you don't like).

• I would be thinking about splitting into "temperature", "wind", and "humidity". These could then be interpreted to imply rain/hail/snow/etc, without ever having a conflict. Dec 5 '11 at 5:00

It sounds like a Hidden Markov Model might be useful to you. These define certain states with statistical probabilities of transitions from one state to each of the other states based on relative frequency in observations.

You can define some weather states for instance 'rainy' and 'sunny' and probabilities of transitions from rainy to sunny and sunny to rainy and calculate for instance the probability that it will soon be raining based on your observation of if it's currently sunny or rainy. The Wikipedia article explains this but this tutorial (PDF) shows a more practical approach.

If you're modelling lots of different weather patterns it would be difficult to model them independently with this pattern, but you can define weather such as 'FogRain' or 'WindSnow' 'SunWind' 'Sun' for instance and have probabilities for each. These probabilities could even be modified based on the in-game month for example. There is a small issue to be aware of though that the size of your probability matrix will increase by n^2 as you add more weather types as each needs to have transition probabilities (even if they're zero) to the others.

• Markov model, not HMM. Dec 5 '11 at 9:50
• +1ed for simplicity. Having something simple, tunable and understandable is a lot better than having something accurate and complex. Plus you can always make it fancier if you want, and make the transition probabilities depend on something (altitude, local biome etc.). Dec 5 '11 at 15:59
• Although I like this, it's slightly inconvenient since other developers would have to think about transitions in probabilities. Combined with the fact that weather is time-of-the-year dependent, Markov model would probably complicate things Dec 5 '11 at 20:51
• Perhaps I misunderstand your suggestion. For every month I'd have to have an n^2 table of transition probabilities, right? Where N is the 2^W of all weather conditions. Thus having 3 weather models I'd have 64 probabilities for a single month Dec 5 '11 at 20:59
• @Mikhail for each month you'd have a table of transition probabilities from each weather condition to each other condition, but this doesn't necessarily have to be N^2 in space complexity as it doesn't have to be a matrix (could be a cyclic graph structure with each branch pointing to one of the next values in the chain with a probability attached). It's not that difficult to conceptualise, but perhaps you might like to split it up by every 2-3 months rather than every month. This article is better than the one I previously linked, might help more! Dec 5 '11 at 21:34

In short..

I'd be thinking this through science.

As we all know, snow is rain and vice versa, except they have different solid states due to temperature changes.

Temperature changes can be associated to winds.

A quick Google for "what causes wind" gave me an answer from Answers.com:

"The pressure gradient force, which arises from the uneven heating of earth's surface. Once it is in motion, the Coriolis force arises from Earth's rotation to deflect it (right in the northern hemisphere, left in the southern hemisphere)."

Aye, not the best source for scientific questions, but it gives the idea.

So, theoretically, you only need wind now. It could be random, or, might as well be somehow scripted to you environment which heats due to some events.

Take wind now, apply to your current position, and randomly or through a scripted way, make clouds out from it. The denser the cloud, the more the probability and density of rain. If temperature is low enough, rain becomes snow. Move the cloud around and make it "not so dense" with that wind.

As for the display, that's up to you, just cache the states of the falling particles, if rain -> use water drop texture and if snow -> use snowflake texture. Use the wind to make these particles move according to wind direction (yeah, you should store this one too, somewhere) to make it a little more realistic.

And with this you can expand upon everything. Like, if there is snow falling and the snowflakes fall near some kind of heat source, change particle texture to rain again.

Lots of possibilities actually, this is just a simple concept.

P.S. As for the hail, look what makes those, expand the concept for such environment.

Have fun, good luck.