This should limit the posts pulled in the new WP_Query instance to the currently selected category or the first category of the current post.
global $wp_query;
$sidebar_query_vars = array('orderby' => 'rand', 'showposts' => 12);
if(is_category()) {
$sidebar_query_vars['cat'] = $wp_query->get_queried_object()->term_id;
} elseif (is_single()) {
$categories = (get_the_terms($wp_query->get_queried_object_id(), 'category'));
if(is_array($categories)) {
$first_cat = array_shift($categories);
$sidebar_query_vars['cat'] = $first_cat->term_id;
}
}
$sidebar_query = new WP_Query($sidebar_query_vars);
while($sidebar_query->have_posts()): $sidebar_query->the_post();
//do output here
endwhile;