If you want to have it both random and tied to magic being cast, here's a thought. Every time a leveled spell is cast (unless you want it to possibly activate from cantrips, too), roll a d10. On a 3 or lower, the magic effect activates. Or a 5 or lower, or on an even or odd roll. Or a 1-in-6 chance. Whichever works best for your game. Maybe have a couple different versions, so one area might only trigger on a 1-in-10 roll, but a more volatile region might trigger on any odd number. That's going to depend on the effect you choose, how deadly it is, and how likely it is to target a specific character.
I'd determine target randomly. If it only targets the caster or the target of a spell, you're making it way more likely for martial characters to not have any chance to get hit by it at all if an enemy caster isn't around. Having it target randomly gives it more of a gambling feel. Do your casters want to cast their powerful spells knowing it might trigger a magic effect on their own party? Or will they give up some of their versatility, but keep their friends safe by only using cantrips?
You could do that by having magic "nodes" where the spell energy is especially concentrated. If an eruption triggers, roll a die to see which node erupts. Anyone within X range of that node, gets hit. I'd keep the number of nodes down at four, so you can use a d4 to determine. Six, tops. And if you can set them on the battlefield so they're overlapping, forcing characters into a bottleneck where they risk getting caught in an eruption, even better. That way they can't just skirt around and avoid the spell effects altogether.
If you tie it to number of spell levels cast, that means you have to keep track of that, which is just one more thing on a host of things that a DM has to keep track of already.