Can I use Plain PHP functions in tpl file? I have a tpl.php page for login form in my drupal site in which have 4 images. 

I need to display only one of these 4 images randomly on each page load.

I can easily do this in less than 10 lines of code using the php rand() function.

But is it advisable to directly use such functions in tpl files.
Could this have some sort of security risk?

UPDATE:

The 4 images that I talked about. I have just uploaded them to sites/default/files/images folder and have included them in tpl file using following code:

&lt;img src="http://mysite.com/sites/default/files/images/image.jpg"/&gt;


I have a feeling that this is unsafe too. Please advise.
For the first part of question:

You may output as form item the output of your custom function, where contain logic to get random image:

function MY_MODULE_form_alter(&amp;$form, &amp;$form_state, $form_id) {
    if ($form_id == 'user_login') {
        $form['img_item'] = array(
          '#type' =&gt; 'markup',
          '#markup' =&gt; my_random_image_output(),
          '#weight' =&gt; -50
        );
    }
}

function my_random_image_output() {
    $output = '';

    // your custom logic to output one image $output = '&lt;img src=".... /&gt;'

    return $output;
}