Last post Jan 25, 2008 11:04 AM by rxwen
Jan 22, 2008 05:57 AM|Spider Master|LINK
Hello and thanks to anyone that can help out.
Randomly selecting an XML item (a single id) based on impressions using an ashx handler (in C#)
Basicly i need the code that ad rotator uses so i can use it to gather a single (id) that is used in the handler.
My current code in .ashx that was calling a file staticly.
private XmlDocument _doc =
_doc = LoadXmlData(context.Server.MapPath(
\\\And now about another 1400 lines of code.
The XML file
Jan 23, 2008 09:51 AM|rxwen|LINK
So your intention is to select an id from the xml file, and the chance of an item being selected is based on the impression value.
I think you can use the following algorithm,
1. Calculate the sum of all items, that's 9999+2344+1211
2. Generate a random number no larger than the sum
3. Return item 1 if the random number is within range 0~9999, return item 2 if it's within range 9999 ~ 9999+2344, and so forth
Jan 25, 2008 05:34 AM|Spider Master|LINK
Hmm thanks for that I have been having a play and the weighting is very un accurate.
I done a small test run (1-10) where as 10 should be displayed 10 times for every 1 should only display once until 10 = 10 if you can understand that lol but what happens is 1 can be displayed 10 times where as 10 sometimes only 5. So it kind of works maybe
something is missing.
Jan 25, 2008 11:04 AM|rxwen|LINK
Here is my implementation of the algorithm. I tested it against 1 and 9, and the chance that 9 got displayed is at approximate 90%.
Random rdm = new Random();
int numOf1 = 0, numOf9 = 0;
for (int i = 0; i < 200; i++)
if (rdm.Next(11) < 10)
Response.Write((float)numOf9 / (numOf9 + numOf1));
Please note that the number of tests should be large enough.