too long

I have a site that I want to display ads to 10% of my traffic. I am getting on average around 30,000 hits a day and want 10% of those users to see an ad from one of my advertisers.

What's the best way to go about implementing this?

I was thinking about counting the visitors in a database, and then every 10 people that visit 1 user gets an ad. Or is there a better way of going about it?

I'm no good with math, so I'm not sure what's the best approach.

Generate a random number between 1 and 10, and compare it to a fixed number, and your code will run on average 10% of the time:

if (rand(1,10) == 1) {
  echo 'ad code';
}

You can make this per-user instead of per-pageview by storing whether that user was 'chosen' in their session.

session_start();
if (isset($_SESSION['show_me_ads']) || rand(1,10) == 1)
  $_SESSION['show_me_ads'] = true;
  echo 'ad code';
}

In its simplest form:

if (rand(1,10) == 1) {
    echo $ad_content;
}
if(rand ( 1,10) == 1)
    display_ads();

You can use

 if(mt_rand(1,10)==10){
      //show your code;
 }

It will show ads to about 10% users

Why would you show ads to a few unlucky ones instead of randomly deciding per page impression (instead of per visitor)?

In php, you can just go ahead and write:

$adPercent = 10;
if (rand(0, 100) < $adPercent) {
   echo '<div class="ads">Buy now!</div>';
}

I use Google's DFP (Doubleclick for Publishers) to serve ads on my site. It's pretty robust. You have to have an AdSense account, but that's not very hard to obtain, it's just annoying to wait to be approved.

Once you have it set up and your ads loaded in, you can control how many people see your ad by percentage (such as the 10% you were talking about), total pageviews, etc.

Look into it: http://google.com/dfp

If you'd rather not use 3rd party software, I'd think the simplest way would be to randomize it so 1/10 visitors see your ad. The simple way would be:

if (rand(1,10) == 1) {
  echo 'YOUR AD CODE HERE';
}

You said you're not good at math, and I understand that, I'm pretty horrible at it too, but basically, every time the page is loaded, it's "rolling" a 10-sided "dice". Every time it "rolls" a 1 (which would be 1 out of 10 times, or 10%), it'll display the ad. Otherwise, it'll be ignored.

The reason this is better than relying on counting the number of users (aside from simplicity) is that it will still roll 1 10% of the time whether you have 30,000 visitors or 3,000,000.

if this was for google ads, then you would need to make the ad insertion optional (using the prob logic above), suggest something along the lines of Google Ads Async (asynchronous)

<script type="text/javascript"><!--
// dynamically Load Ads out-of-band
setTimeout((function ()
{
    // placeholder for ads
        var eleAds = document.createElement("ads");  
        // dynamic script element
        var eleScript = document.createElement("script");  
        // remember the implementation of document.write function
        w = document.write;
        // override and replace with our version
        document.write = (function(params)
        {
        // replace our placeholder with real ads
        eleAds.innerHTML = params;
        // put the old implementation back in place
        document.write=w;
        });
        // setup the ads script element
        eleScript.setAttribute("type", "text/javascript");
        eleScript.setAttribute("src", "http://pagead2.googlesyndication.com/pagead/show_ads.js");
        // add the two elements, causing the ads script to run
        document.body.appendChild(eleAds);              
        document.body.appendChild(eleScript);           
}), 1);
                //-->
        </script>