Off the top of my head, add the image name to an array, you could query one at a time in the if/else, or grab a large enough random sample that you would be able to display 5 with a very minimal chance of a duplicate, say 75-ish
<?php
for ($i = 1; $i <= 5; $i++)
{
if (in_array($name,$imagesdisplayed))
{
//query another random or skip to next row here
else
{
$imagesdisplayed[$i]=$name
///run your display routine here
} /end if if-else
} // end of for loop
?>