Like before, the best way is to fetch the image count with list_count or tree_count. Then generate a random number between 0 and count -1, and fetch the image with the random number as offset and limit = 1 using list of tree fetch(depending on what you used to get image count).
Thanks for your kind help and uploading the contribution, would it be possible to have some idea what changes you made to make it work on ezpublish4.0.
I am in the process of upgrading few sites from 3.6.0 to 4.0.