If you’re using AdSense or affiliate ads on your website for monetizing, have you ever wanted to display random ads to your visitors, instead of boring them by showing the same ad again and again? Mostly, ads can be placed on website by putting respective ad code from the related Advertising services (like AdSense, Tribal Fusion, Amazon, eBay, Google Affiliate Network etc). And yes, these ads can be randomized!

I basically do this by using simple JavaScript: the ads will be randomized every time when the page reloads. People, who are looking how to show random ads on their website or blog, may follow these steps to do so.

  • Open the HTML template of your website. Copy the one of below given CODE 1 and CODE 2 according to your need and paste it just before </head> tag in your blog or website.

    Code 1: To display 2 random ads:

    <script type="text/javascript">
        var ad1 = 'AD_1';
        var ad2 = 'AD_2';
        function show() {
            var the_ads = Math.random();
            if(the_ads < .5) document.write(ad1);         else document.write(ad2);     } </script>

    Code 2: To display 4 random ads:

    <script type="text/javascript">
        var ad1 = 'AD_1';
        var ad2 = 'AD_2';
        var ad3 = 'AD_3';
        var ad4 = 'AD_4';
        function showads() {
            var the_ads = Math.random();
            if(the_ads < .25) document.write(ad1);         else if(the_ads > .25 && the_ads < .5) document.write(ad2);         else if(the_ads > .5 && the_ads < .75) document.write(ad3);         else document.write(ad4);     } </script>

  • Don't forget to replace AD_1, AD_2, AD_3 and AD_4 with the code of your respective affiliate ads (forget AD_3 and AD_4 if using Code 1)
  • Paste below given code where you want to display ads:
    <script type="text/javascript">
    showads();
    </script>
  • Save website/blog template. You're done.

Hope you find this post useful. Also see how to implement such ad randomizing to increase AdSense CTR.