Recommended hosting
Aug 21 2007

Selecting random items from array

Posted by admin under .NET

I recently had to do a hack on a site, and implement random link rolling. The used a third party CMS so all I could do (well considering the time parameter as well) was to change the ASP.NET master page to include some quick link selection code as well.

So therefore this code might look very hardcoded - I mean a "real" solution would probably implement some sort of database backend for it, but nevermind. I did it and now I publish it:)

The basic idea is to create some sort of structure or class for links (cause regular strings won't do - I wanted them to have a text AND the url property). Then fill an Arraylist up and do the randomization, returning a new ArrayList.



    struct QuickTestLink
    {
        public string Link;
        public string Text;
        public QuickTestLink(string sLink, string sText)
        {
            Link = sLink;
            Text = sText;
        }
    }












There's the structure - now lets look at the GetRandomLinks function:



    public ArrayList GetRandomLinks(int nLinks)
    {
        System.Collections.ArrayList oArrInput = new ArrayList();
        oArrInput.Add(new QuickTestLink("http://www.systementor.se", "(C) Systementor AB 2007"));
        oArrInput.Add(new QuickTestLink("http://www.aspcode.net", "Free ASP.NET scripts"));
        oArrInput.Add(new QuickTestLink("http://www.programmingado.net", "ADO.NET programming"));
        oArrInput.Add(new QuickTestLink("http://www.ajaxandasp.net", "Ajax and ASP.NET"));
        oArrInput.Add(new QuickTestLink("http://www.aspcode.net/Ajax.aspx", "Ajax and JQuery"));
        oArrInput.Add(new QuickTestLink("http://games.aspcode.net", "ASPCode games"));

        if ( nLinks >= oArrInput.Count )
            return oArrInput;

        System.Random oRand = new Random();
        
        //Find out qhat quickest? To randomize the ones to go or the ones to keep?
        if (nLinks >= oArrInput.Count / 2)
        {
            //We are trying to get more than half, so lets randomize some to go...
            while (oArrInput.Count > nLinks)
                oArrInput.RemoveAt(oRand.Next(0,oArrInput.Count-1));
            return oArrInput;
            
        }
        else
        {
            System.Collections.ArrayList oArrReturn = new ArrayList();
            while (oArrReturn.Count < nLinks)
            {
                //Randomize one
                int nIndexToMove = oRand.Next(0, oArrInput.Count - 1);
                oArrReturn.Add(oArrInput[nIndexToMove]);
                oArrInput.RemoveAt(nIndexToMove);

            }
            return oArrReturn;
        }




I tried to do some optimization here. If the initial array is 10 elements and we are requesting 9 random it's quicker to remove one single item from the original array and return that.

On the other hand, if we just want two random items out of 10 it's best to select those into a new array, I reckon.

 

 

ASPCode.net recommends