locked
using ashx handler to get data from an xml file based on impressions RRS feed

  • Question

  • User-122480877 posted

    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 = null;

    public void ProcessRequest(HttpContext context)

    {

    _doc = LoadXmlData(context.Server.MapPath("~/Collections/ Returned id goes here with .xml extension ".xml));

                                                                     ///  I assume I would use --> " + GetCollectionsId() + " 

    context.Response.Clear();

    context.Response.ContentType =
    "text/javascript";

    context.Response.Write(MakeMenu(GetItemsList()));

    StreamReader sr = new StreamReader(context.Server.MapPath("~/PoweredMenu2008.js"));

    context.Response.Write(sr.ReadToEnd());

    sr.Close();

    }

    \\\And now about another 1400 lines of code. 

     

    The XML file

    <items>

    <item impression="9999" id="0372"/>

    <item impression="2344" id="0373"/>

    <item impression="1211" id="0374"/>

    </items>

    Tuesday, January 22, 2008 5:57 AM

Answers

  • User-893397986 posted

     Hi,

    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

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, January 23, 2008 9:51 AM
  • User-893397986 posted

    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)
                    ++numOf9;
                else
                    ++numOf1;
               
            }

            Response.Write((float)numOf9 / (numOf9 + numOf1));

     

    Please note that the number of tests should be large enough. 

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, January 25, 2008 11:04 AM

All replies

  • User-893397986 posted

     Hi,

    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

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, January 23, 2008 9:51 AM
  • User-122480877 posted

    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.

     

    Friday, January 25, 2008 5:34 AM
  • User-893397986 posted

    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)
                    ++numOf9;
                else
                    ++numOf1;
               
            }

            Response.Write((float)numOf9 / (numOf9 + numOf1));

     

    Please note that the number of tests should be large enough. 

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, January 25, 2008 11:04 AM