Embeding style into the header via the function.php

You are not escaping your PHP correctly. In this part $output="... <?php
You could instead do it like this:

add_action('wp_head','hook_css');
function hook_css()
{
?> <!-- Closing the PHP here -->
    <style>
        body {
        background-repeat: no-repeat;
        background-attachment: fixed;
        background-size: 100% 100%;
        background: url('../images/backgrounds/bg<?php echo rand(1,4); ?>.jpg') no-repeat top center;
        }
    </style>
<?php //Opening the PHP tag again
}

Or if you prefer to have it the way you do with the $output variable – Make sure to escape correctly around your rand() function. Like this: ...backgrounds/bg". rand(1,4) .".jpg