How Do I Alter This Random Image Script?

I have a script that inserts a random image from an array and I am wanting to alter the script so that I can make each image wrapped in an anchor tag. Once the image is inserted by the javascript, I want to be able to make it clickable and tell it what URL to go to.

Here is the script:

var imagenumb = 2 ;
var randomnumb = Math.random() ;
var rand1 = Math.round( (imagenumb-1) * randomnumb) + 1 ;
img = new Array
img[1] = "i/gfx_slide1.jpg"
img[2] = "i/gfx_slide2.jpg"
var img = img[rand1]

var p = Pic.length;
var preLoad = new Array();
for (i = 0; i < p; i++){
   preLoad[i] = new Image();
   preLoad[i].src = Pic[i];

And here is its call in the HTML:

<script language="JavaScript">document.write('<img src="' + img + '">')</script>