How do I sort products on a category page by subcategory as well as limit the number of products from each subcategory: 

For example if the category was Food I would want to display the following:

Drinks
Coke 12oz, Orange Juice 8oz, Milk Gallon,

Pasta,
Spaghetti 1lb, Pesto 12 pc, Tortellini 1 PC.

And so on, displaying each subcategory name followed 3 products (images etc.)

I currently have a custom template that displays the subcategories but can't figure out the products. Here is my current code:

&lt;?php
    $_category = $this-&gt;getCurrentCategory();
    $collection = $_category-&gt;getCollection()
        -&gt;addAttributeToSelect(
            array('url_key','name','all_children','is_anchor','description','image')
        )
        -&gt;addAttributeToFilter('is_active', 1)
        -&gt;addIdFilter($_category-&gt;getChildren())
        -&gt;setOrder('position', 'ASC')
        -&gt;joinUrlRewrite();

    $helper = Mage::helper('catalog/category');
?&gt;

&lt;ul&gt;
&lt;?php foreach ($collection as $cat): ?&gt;
&lt;li&gt;
    &lt;div class="level1descript"&gt;
        &lt;a href="&lt;?php echo $helper-&gt;getCategoryUrl($cat); ?&gt;"&gt;
            &lt;img src="&lt;?php echo $cat-&gt;getImageUrl(); ?&gt;" class="catlevel1image" /&gt;
            &lt;h2&gt;&lt;?php echo $cat-&gt;getName(); ?&gt;&lt;/h2&gt;
        &lt;/a&gt;
        &lt;p class="level1descript"&gt;
        &lt;?php
            $catdesc = '';
            $catdesc = strip_tags($cat-&gt;getDescription());
            if (strlen($catdesc) &gt; 300) {
                $catdesc = substr($catdesc, 0, 300) . ' ...';
            }
            echo $catdesc;
        ?&gt;
        &lt;/p&gt;
    &lt;/div&gt;
    &lt;?php 

        $childLevel2Category = $cat-&gt;getCollection()
            -&gt;addAttributeToSelect(
                array('url_key','name','all_children','is_anchor','description','image')
            )
            -&gt;addAttributeToFilter('is_active', 1)
            -&gt;addIdFilter($cat-&gt;getChildren())
            -&gt;setOrder('position', 'ASC')
            -&gt;joinUrlRewrite();
    ?&gt;
    &lt;ul&gt;
        &lt;?php foreach ($childLevel2Category as $catLevel2) { ?&gt;
        &lt;li class="level2cats"&gt;
            &lt;a href="&lt;?php echo $helper-&gt;getCategoryUrl($catLevel2); ?&gt;"&gt;
                &lt;img src="&lt;?php echo $catLevel2-&gt;getImageUrl(); ?&gt;" class="catlevel2image" /&gt;
                &lt;h4&gt;&lt;?php echo $catLevel2-&gt;getName(); ?&gt;&lt;/h4&gt;
            &lt;/a&gt;
            &lt;p class="level2descript"&gt;
            &lt;?php
                $catdesc = '';
                $catdesc = strip_tags($catLevel2-&gt;getDescription());
                if (strlen($catdesc) &gt; 60) {
                    $catdesc = substr($catdesc, 0, 60) . ' ...';
                }
                echo $catdesc;
            ?&gt;
        &lt;/li&gt;
        &lt;?php } ?&gt;
    &lt;/ul&gt;
&lt;/li&gt;
&lt;?php endforeach;?&gt;
&lt;/ul&gt;



Question: How to easily determine the results distribution for multiple dice?
Answer: Exact solutions

The number of combinations in $n$ throws is of course $6^n$.

These calculations are most readily done using the probability generating function for one die,

$$p(x) = x + x^2 + x^3 + x^4 + x^5 + x^6 = x \frac{1-x^6}{1-x}.$$

(Actually this is $6$ times the pgf--I'll take care of the factor of $6$ at the end.)

The pgf for $n$ rolls is $p(x)^n$.  We can calculate this fairly directly--it's not a closed form but it's a useful one--using the Binomial Theorem:

$$p(x)^n = x^n (1 - x^6)^n (1 - x)^{-n}$$

$$= x^n (\sum_{k=0}^{n} {n \choose k} (-1)^k x^{6k} )( \sum_{j=0}^{\infty} {-n \choose j} (-1)^j x^j).$$

The number of ways to obtain a sum equal to $m$ on the dice is the coefficient of $x^m$ in this product, which we can isolate as

$$\sum_{6k + j = m - n} {n \choose k}{-n \choose j}(-1)^{k+j}.$$

The sum is over all nonnegative $k$ and $j$ for which $6k + j = m - n$; it therefore is finite and has only about $(m-n)/6$ terms.  For example, the number of ways to total $m = 14$ in $n = 3$ throws is a sum of just two terms, because $11 = 14-3$ can be written only as $6 \cdot 0 + 11$ and $6 \cdot 1 + 5$:

$$-{3 \choose 0} {-3 \choose 11} + {3 \choose 1}{-3 \choose 5}$$

$$= 1 \frac{(-3)(-4)\cdots(-13)}{11!} + 3 \frac{(-3)(-4)\cdots(-7)}{5!}$$

$$= \frac{1}{2} 12 \cdot 13 - \frac{3}{2} 6 \cdot 7 = 15.$$

(You can also be clever and note that the answer will be the same for $m = 7$ by the symmetry 1 &lt;--> 6, 2 &lt;--> 5, and 3 &lt;--> 4 and there's only one way to expand $7 - 3$ as $6 k + j$; namely, with $k = 0$ and $j = 4$, giving

$$ {3 \choose 0}{-3 \choose 4} = 15 \text{.)}$$

The probability therefore equals $15/6^3$ = $5/36$, about 14%.

By the time this gets painful, the Central Limit Theorem provides good approximations (at least to the central terms where $m$ is between $\frac{7 n}{2} - 3 \sqrt{n}$ and $\frac{7 n}{2} + 3 \sqrt{n}$: on a relative basis, the approximations it affords for the tail values get worse and worse as $n$ grows large).

I see that this formula is given in the Wikipedia article Srikant references but no justification is supplied nor are examples given.  If perchance this approach looks too abstract, fire up your favorite computer algebra system and ask it to expand the $n^{\text{th}}$ power of $x + x^2 + \cdots + x^6$: you can read the whole set of values right off.  E.g., a Mathematica one-liner is

With[{n=3}, CoefficientList[Expand[(x + x^2 + x^3 + x^4 + x^5 + x^6)^n], x]]