at the top of your script you could create an array of paths to folders with images in it and do the array_rand() function to grab a random folder, then where you have
$folder=opendir("rndimgs/");
it could be
$folder=opendir($folders[$random_folder]);
you seem to know how to use the array_rand function in there below so you can probably understand what i mean above but if not:
$folders = array("/path/to/images1/","/path/to/folder/moreimages/","/path/to/folder/otherimages/");
$random_folder = array_rand ($folders);
just insert that at the top and be sure to move your call to the random number generator to the top of the program. hope that helps