{"id":829,"date":"2019-02-20T12:23:40","date_gmt":"2019-02-20T17:23:40","guid":{"rendered":"https:\/\/resrvoir.com\/?page_id=829"},"modified":"2025-06-18T23:12:20","modified_gmt":"2025-06-19T03:12:20","slug":"ad-impressions","status":"publish","type":"page","link":"https:\/\/resrvoir.com\/?page_id=829","title":{"rendered":"Ad Impressions (easy)"},"content":{"rendered":"\r\n<p>There are three ads: A, B, and C. Ad A has paid for 60M impressions, B 30M impressions, and C 10M impressions. All ads are scheduled to run during the same time period. Try to show all ads as fairly as possible.<br \/><br \/>One approach would be to show all A first, then all of B next, then C. This however means B can&#8217;t be shown until all of A are finished, and C can&#8217;t be shown until all of B are finished.<br \/><br \/>A better distribution is to randomize the selection and each ad should be selected some percent of the whole, in this case 60% for A, 30% for B, and 10% for C. Allocate an array of 10 elements, the first 6 elements contain A, the next 3 contain B, and the last 1 contains C. A random selection between 0 and 9 from the array determines which ad to serve next. Here is pseudocode:<\/p>\r\n\r\n\r\n\r\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"kotlin\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">id adIds = [a,a,a,a,a,a,b,b,b,c]\r\n\r\nid getNextAd(adIds) {\r\n  adIndex = random() % adIds.length\r\n  adToShow = adIds[adIndex]\r\n  return adToShow\r\n}<\/pre>\r\n\r\n\r\n\r\n<p>This yields a nice weighted random distribution for ads scheduled to run during the same time period. Searching for the next ad to show is O(1) time.<\/p>\r\n","protected":false},"excerpt":{"rendered":"<p>There are three ads: A, B, and C. Ad A has paid for 60M impressions, B 30M impressions, and C 10M impressions. All ads are scheduled to run during the same time period. Try to show all ads as fairly as possible. One approach would be to show all A first, then all of B [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":2,"menu_order":16,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"_links":{"self":[{"href":"https:\/\/resrvoir.com\/index.php?rest_route=\/wp\/v2\/pages\/829"}],"collection":[{"href":"https:\/\/resrvoir.com\/index.php?rest_route=\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/resrvoir.com\/index.php?rest_route=\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/resrvoir.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/resrvoir.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=829"}],"version-history":[{"count":33,"href":"https:\/\/resrvoir.com\/index.php?rest_route=\/wp\/v2\/pages\/829\/revisions"}],"predecessor-version":[{"id":1424,"href":"https:\/\/resrvoir.com\/index.php?rest_route=\/wp\/v2\/pages\/829\/revisions\/1424"}],"up":[{"embeddable":true,"href":"https:\/\/resrvoir.com\/index.php?rest_route=\/wp\/v2\/pages\/2"}],"wp:attachment":[{"href":"https:\/\/resrvoir.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=829"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}