Random category with recent post title and thumbnail

If I’m not wrong a the_post() function will take the post info into the loop. If so, then the the_title() and the_post_thumbnail() will do the rest for you.

<?php
//display random sorted list of terms in a given taxonomy
$counter = 0;
$max = 5; //number of categories to display
$taxonomy = 'category';
$terms = get_terms($taxonomy);
shuffle ($terms);

the_post();
if ($terms) {
foreach($terms as $term)
    {
        $counter++;
        if ($counter <= $max)
        {
            echo '<p><a href="' . get_category_link( $term->term_id ) . '" title="' . sprintf( __( "View all posts in %s" ), $term->name ) . '" ' . '>' . $term->name.'</a></p> ';
            echo '<p><a href="' . the_permalink() . '">' . the_title() . '</a></p>';
            echo '<p><a href="' . the_permalink() . '">' . the_post_thumbnail( 'thumbnail' ) . '</a></p>';
        } //endif ($counter <= $max)
    }
} //endif ($terms)
?>

NOTE: I haven’t tested it. Please let me know whether it works or not.