# To control the display of different items on varying periodicity for a web applicatio

Smitha S Nair
Greenhorn
Posts: 1
We need to implement a logic for a web application where in we need to control the display of some items with varying frequency/percent. For eg. let us say I need show an ad a specific percentage of users. There might be different products under different brands and I have to maintain a certain percentage for the combination of brand/product. I could define the percentage for each brand/product in an XML/properties file. The question is how to ensure the ads do stick to this.

One way would be to maintain an application wide session to track the user counts and maintain a brand/product combination ad hit to ensure a certain percentage/number.

But with a growing number of brand/product combination, the session object might get too huge to manage and also might affect the performance

We also tried to use random number and check agains a specified percentage and determine if the current session falls under this percentage (A sample program below). But there was a variation of about 10%.

Please suggest if you are aware of a better logic of achieving this, so that more accurately stick to the percentages.

This is the sample code we tried, please note that the count below is just an indicative one which would be replacing the actual code and hence cannot be used to tweak to stick to the percentage.

public class TestRandomTwo{
public static void main(String args[]) {
int countA = 0;
int percentA = 30;
int countB = 0;
int percentB = 100;
int countC = 0;
int percentC = 50;

for(int i=0;i<100;i++) {
int rand = (int)(Math.random()*100);
if(percentA>=rand)
countA ++;
if(percentB>=rand)
countB ++;
if(percentC>=rand)
countC ++;
}
System.out.println("count beyond "+percentA+"% = "+countA);
System.out.println("count beyond "+percentB+"% = "+countB);
System.out.println("count beyond "+percentC+"% = "+countC);
}
}

Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
Hi, welcome to the ranch!

But there was a variation of about 10%.

Yup, random does not guarantees a perfect distribution. Delivering add x on exactly n% of page displays is impossible. After one sample it will be 0% or 100%. Oh my!

I have used a scheme with "weights" instead of percentages. You could buy more weight, but if your competitor buys the same amount the percentage might be 50%. I was trying to encourage weight building wars.

Can that work?