# Talk:Horn of plenty
Probabilities
The selection of which item a horn of plenty gives you is done in the function doapply in apply.c via a call to mkobj in mkobj.c after picking potion (1/7 chance) or food (6/7 chance). mkobj picks from the entire class based on the priorities in objects.c; doapply then modifies that selection. For potions, if a magical potion is initially selected, it is changed via a call to rnd_class in objnam.c. Additionally rnd_class called repeatedly until something other than sickness is selected. The original potion probabilities on the page assumed rnd_class uses equal weight in making its selection. However, it uses the probability weights as mkobj. For example water has a 92 (out of 1000) probability, the magical potions have a combined probability of 742, and the non-magical potions excluding sickness have a combined probability of 216. If you assume rnd_class selects equally you get 9.2 + 74.2 / 5 = 24. If probabilities are applied as they are in rnd_class you get 9.2 + 74.2 * 92 / 216 = 40.8. When I changed numbers on the page I added links to the source code mentioned above. Also, I thought it would be useful to have overall probabilities, which is simply a matter of multiplying the class numbers by 1/7 for potions and 6/7 for food. --Skidragon2 (talk) 17:47, 7 October 2012 (UTC)

Thank you for that explanation! I made the function names in your above text link to their definitions in the source code; I hope that's OK. --Bcode (talk) 18:06, 7 October 2012 (UTC)
Hey that if...do...while bit in doapply is a nifty little construction, thanks for digging it up and puzzling it out. Nice source diving&nbsp;:). Am I reading this right, that the only way for a potion of sickness to appear is for it to be selected on the first roll? And that's what accounts for the low probability of sickness? (4.2% of potions, even though its probability for usual random generation is 42/1000; while oil's probability for usual random generation is only 30/1000, but it accounts for 13.3% of potions from the horn) Kind of a weird way to implement it, but that's the devteam for ya. Blackcustard (talk) 00:42, 8 October 2012 (UTC)