(Ideally, this code could go wherever your mod's initialization method is)
In the above code, when a chest's contents are generated, it will try between 7-8 times (exclusive of max parameter) to select a random item to add to the inventory.
WeightedRandomChestContent has 2 constructors
WeightedRandomChestContent(int itemId, int damage, int minChance, int maxChance, int itemWeight)
WeightedRandomChestContent(ItemStack itemStack, int minChance, int maxChance, int itemWeight)
When a WeightedRandomChestContent entry is selected for generation, it will produce between minChance and maxChance items of that type (inclusive).
The probability of an entry being selected is the itemWeight for a particular item divided by the combined itemWeight for all the entries.
In the above example, the combined item weight is 35, so cocoa beans would have a probability of 10/35 (28.6%) of being selected.
To actually generate the items in your chest, you would use this code
TileEntity te = world.getBlockTileEntity(x, y, z);
if (te instanceof IInventory)
ChestGenHooks info = ChestGenHooks.getInfo("Category");
WeightedRandomChestContent.generateChestContents(random, info.getItems(random), te, info.getCount(random));
Note the "Category" string, this name should be unique. 'world' and 'random' are your standard World and Random parameters, and x, y, z are is the position at which you previously created the chest.
This should work for any chest that implements IInventory.