How to optimize the scenario of getting a random picture?
<p>I have about 100 pages, every of which has 10-100 images attached. The path to the images are kept in a database.</p>

<p>Then, I have an area at every page where user can see random pictures from the list of pictures mentioned above. This image changes every 3 seconds.</p>

<p>To archieve such scenario I use a javascript function, which calls itself every 3 seconds.</p>

<pre><code>function GenerateNewImg() {
    $.ajax({
        url: "myurl.php",
        type : "get",
        dataType: 'json',
        async:true,
        success: function(data){
            $("#imgtochange").attr("src", data.res);
        }
    });

    t = setTimeout('GenerateNewImg()',3000);
}
</code></pre>

<p>And in myurl.php I randomly choose a page and then an image.</p>

<p>I think, this is not very good solution, because it consumes processor time at the server.</p>

<p>Are there better ways to get a behaviour I need?</p>
