I don't really understand the code.. Can you maybe simplify it a bit..
Something like
<?
// code to make it random
?.....?
// array of links and images
ad ("<a href="link1.html"><IMG SRC="pic1.gif"></A>,
"<a href="link2.html"><IMG SRC="pic2.gif"></A>,",
"<a href="link3.html"><IMG SRC="pic3.gif"></A>,");
?>
Then I just put
<?php echo "$ad"; ?>
were I want the banner to be displayed.