I'm tempted to write a cleaner, more flexible C version
I have written a C program which changes the background to a random image from a specified directory. However, it does not immediately change it, although that's easy to do. The new image will show after a reboot. I run the program from my .profile.

Best Wishes
Pete