This code, when given a category id, will return a random product id (or 0 if none are found) from the products that belong to the selected category id AND ALL CHILD CATEGORIES BELOW IT.

So, if you have a category 1 = food, 11 = hot food, 12 = cold food, 13 = soup, 14 = entrees, and you pass category id 1 to the main function, you will get back a random product from the products of categories 1, 11, 12, 13 and 14. If you pass 11, the product will be from categories 11, 13 and 14.

This is a set of 3 functions in one file, categories.php, which should be placed in catalog/includes/functions. When you open the file, you will see at the top the selection parameters that can be used and one Define that you may need to change.

More...