A.1 If you want use random see images from the DB do next all images in array and use shuffle command.
A.2 About some space between images
solution 1: use dreamweaver for this (faster and simply)
solution 2: see maby other cell in youre table have a size more the other
solution 3: if S1 and S2 not help you see solution 1