function randomNumber(range) {
    if (Math.random) {
        return Math.round(Math.random() * (range-1));
    } else {
        var now = new Date();
        return (now.getTime() / 1000) % range;
    }
}

function writeAttribute(attr, value) {
  var attribute = attr + '="' + value + '" ';
  return attribute;
}

function randomImage(imageArray, width, height) {
    var img = imageArray[randomNumber(imageArray.length)];
    var attrs = writeAttribute('src', img.src) + writeAttribute('alt', img.alt);
    if (width != null) { 
      attrs += writeAttribute('width', width);
    }
    if (height != null) {
      attrs += writeAttribute('height', height);

    } 
    document.write('<img ' + attrs + '/>');
    document.close();   
}
