this Just in

Home  >>  Blog  >>  Post Detail

We have a winner!

We have a winner for our Halloween iTunes gift card giveaway!

Congratulations to GiraffeDiva (Dana), the winner of our $25 iTunes gift card.

Of course you’re curious of how we selected the winner. Well since we’re software developers after all, we decided we would write a program to do it. If you like you may take a look at the page we created here.

Here is how it works:

  1. Input a list of names with each name on a new line.
  2. Click the “Get Winner” button.

The page takes the list of names and sorts them into a random order, then it picks one of the names at random. Of course, with the more names the more random it will get.

Feel free to use our page to for your own contests, or if you would like to make your own page here is some code to get you going:

ASP.NET Code:

   1: <!-- Text Area to collect names !-->
   2: <asp:TextBox id="txtNames" runat="server" TextMode="multiline" validationgroup="winner" CssClass="field" /><br />
   3:  
   4: <!-- Submit Button !-->
   5: <asp:Button id="btnSubmit" runat="server" Text="Get Winner"  OnClick="btnSubmit_Click" validationgroup="winner" /><br /><br />
   6:  
   7: <!-- Label to display Winner !-->
   8: <asp:Label id="lblWinner" runat="Server" CssClass="winner" visible="false" />

C# Code:

   1: using System;
   2: using System.Collections.Generic;
   3: using System.Web;
   4: using System.Web.UI;
   5: using System.Web.UI.WebControls;
   6:  
   7: public partial class randomwinner : System.Web.UI.Page
   8: {
   9:     protected void btnSubmit_Click(object sender, EventArgs e)
  10:     {
  11:         // Get Random Winner        
  12:         List<string> names = new List<string>();
  13:         names.AddRange( txtNames.Text.Split('\n') );
  14:  
  15:         // shuffle list 
  16:         names = MixList(names);
  17:  
  18:         // Now pick a random item from the shuffled list
  19:         int count = names.Count;        
  20:         Random RandomNumber = new Random();
  21:         int x = RandomNumber.Next(0, count-1);
  22:  
  23:         lblWinner.Text = "And the winner is: " + names[x];
  24:         lblWinner.Visible = true;
  25:     }
  26:  
  27:     private List<string> MixList(List<string> inputList)
  28:     {
  29:          List<string> randomList = new List<string>();
  30:          if (inputList.Count == 0)
  31:               return randomList;
  32:  
  33:          Random r = new Random();
  34:          int randomIndex = 0;
  35:          while (inputList.Count > 0)
  36:          {
  37:               randomIndex = r.Next(0, inputList.Count); //Choose a random object in the list
  38:               randomList.Add(inputList[randomIndex]); //add it to the new, random list<
  39:               inputList.RemoveAt(randomIndex); //remove to avoid duplicates
  40:          }
  41:  
  42:          //clean up
  43:          inputList.Clear();
  44:          inputList = null;
  45:          r = null;
  46:  
  47:          return randomList; //return the new random list
  48:     }
  49: }

 

The code is pretty basic. There’s not a lot of error checking in there, but since we were the ones using it, it worked well.

comments powered by Disqus

Tags

Archive


Like what you see? Have a question for us? Contact Us.

Call us toll free at 1.888.262.9212