Selecting related posts

if there are no 4 related posts, the code below will fill the number with random posts of random categories, and avoid duplicates:

        <div class="my-related-posts"><h4>You may also be interested in...</h4><ul class="relatedposts">
<?php
$duplicates = array( $current_post );
$posts = get_posts( array( "posts_per_page' => 4, 'orderby' => 'rand', 'category' => $category->term_id, 'post__not_in' => array( $current_post )) );
$second_numbers = (!$posts) ? 4 : (4 - count( $posts ));
if( $posts ) :
    foreach($posts as $post) :
    setup_postdata( $post );
    $duplicates[] = $post->ID;
    $image = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ));
    ?>
    <li><a href="https://wordpress.stackexchange.com/questions/89303/<?php the_permalink(); ?>"><?php the_title(); ?><br /><img src="<?php echo esc_url( $image[0] ); ?>" alt="View more info" /></a></li>
    <?php endforeach; wp_reset_postdata(); 
endif; 

if( $second_numbers >=1 ) : 
    $posts = get_posts( array('posts_per_page' => $second_numbers, 'orderby' => 'rand', 'post__not_in' => $duplicates ) );
    if( $posts ) foreach($posts as $post) :
    setup_postdata( $post );
    $image = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ));
?>
    <li><a href="https://wordpress.stackexchange.com/questions/89303/<?php the_permalink(); ?>"><?php the_title(); ?><br /><img src="<?php echo esc_url( $image[0] ); ?>" alt="View more info" /></a></li>
    <?php endforeach; wp_reset_postdata(); 
endif; ?>

</ul>
</div>