How are the Gem loots calculated?

I've noticed that Free Chests and Crown Chests may give Gems to the player. I've also noticed that this number may vary, and the Free Chests may often not give any Gems at all. How are Gem loots calculated?

The number of gems is randomized, even the gems card itself has a chance to appear (or not to). But according to the Clash Royale Wiki section for gems:

Free chests and Crown chests are the only chests to contain gems, containing 2-4 gems.

Free chests have a chance of giving you gems but Crown Chests will always give you gems:

Free Chests don't always contain Gems, but Crown Chests always do.

• While the wiki states this, for the record I for one have yet to ever receive 4 gems. It may be possible, but after hundreds of pulls it seems fleetingly likely (to me). – EBongo Apr 3 '16 at 4:05
• It's important to note that only Crown Chests can contain more than 2 Gems. – John Apr 3 '16 at 13:20
• @EBongo It once happened to me to have 4 Gems in a Crown Chest, so it's possible. – John Apr 3 '16 at 13:22
• @PythonMaster Yeah, I've started to see them very occasionally. I wonder a bit if it has to do with being in Royal, but probably not. Much of the RNG can play tricks on the mind with confirmation bias. – EBongo Apr 24 '16 at 12:36

Some chests tell you how much gems you can get from a chest e.g (10-20) I believe the amount of gems you get is randomized in a pre-determined field for each chest, sort of like how it randomly chooses which cards you get.

• Um... Gems not Gold. – aytimothy Mar 31 '16 at 8:37
• @aytimothy the same thing applies to gems – Data Mar 31 '16 at 9:56
• Also, where does it show that `how much gems you can get from a chest e.g (10-20)`? – aytimothy Mar 31 '16 at 11:14