Image Image Image Image Image
Scroll to Top

To Top

C#

C# Elo Rating Class used on Facemash as seen in the Social Network Movie

On 07, Nov 2010 | 24 Comments | In C#, Programming, Website | By luke


If you wondered what that highly complicated and fancy Algorithm on the Social Network movie is wonder no more.

Turns out it the common rating algorithm often used in chess called the Elo rating system.
After some Googling (yes its a verb), it would appear that the Facemash algorithm corresponds to the Elo rating system. Thus, the equations involved are:


see wikipedia for more explaination or http://gobase.org/studying/articles/elo/ for another good explanation
Apparently programmers write the code on the window of the dorms personally I’ve never done that I just write it on my blog. Anywho.

The class is below written in C# it uses a constant of 400 you can adjust this if you like

public class EloRating
    {
        public double Point1 { get; set; }
        public double Point2 { get; set; }

        public double FinalResult1 { get; set; }
        public double FinalResult2 { get; set; }

        public EloRating(double CurrentRating1, double CurrentRating2, double Score1, double Score2)
        {
            /*
            double CurrentR1 = 1500.0;
            double CurrentR2 = 1500.0;

            double Score1 = 20.0;
            double Score2 = 10;
            */

            double E = 0;

            if (Score1 != Score2)
            {
                if (Score1 > Score2)
                {
                    E = 120 - Math.Round(1 / (1 + Math.Pow(10, ((CurrentRating2 - CurrentRating1) / 400))) * 120);
                    FinalResult1 = CurrentRating1 + E;
                    FinalResult2 = CurrentRating2 - E;
                }
                else
                {
                    E = 120 - Math.Round(1 / (1 + Math.Pow(10, ((CurrentRating1 - CurrentRating2) / 400))) * 120);
                    FinalResult1 = CurrentRating1 - E;
                    FinalResult2 = CurrentRating2 + E;
                }
            }
            else
            {
                if (CurrentRating1 == CurrentRating2)
                {
                    FinalResult1 = CurrentRating1;
                    FinalResult2 = CurrentRating2;
                }
                else
                {
                    if (CurrentRating1 > CurrentRating2)
                    {
                        E = (120 - Math.Round(1 / (1 + Math.Pow(10, ((CurrentRating1 - CurrentRating2) / 400))) * 120)) - (120 - Math.Round(1 / (1 + Math.Pow(10, ((CurrentRating2 - CurrentRating1) / 400))) * 120));
                        FinalResult1 = CurrentRating1 - E;
                        FinalResult2 = CurrentRating2 + E;
                    }
                    else
                    {
                        E = (120 - Math.Round(1 / (1 + Math.Pow(10, ((CurrentRating2 - CurrentRating1) / 400))) * 120)) - (120 - Math.Round(1 / (1 + Math.Pow(10, ((CurrentRating1 - CurrentRating2) / 400))) * 120));
                        FinalResult1 = CurrentRating1 + E;
                        FinalResult2 = CurrentRating2 - E;
                    }
                }
            }
            Point1 = FinalResult1 - CurrentRating1;
            Point2 = FinalResult2 - CurrentRating2;

        }
    }

Comments

  1. Any updates on the site since you last wrote the original code?

    • Geoff Bourhill

      Whassup gangsta! Me and ma homies just chillin in tha mothafokin criiiiaaaybbb

      • luke

        Brilliant!

  2. What is the point in the ternary operator on lines 59/60 when the true/false operations are the same?

    • luke

      the calculations have to be done on both Point1 and Point2
      check the variable names are all different you’ll see what I mean

      • Michael

        Point1 = (((FinalResult1 – CurrentRating1) > 0) ? (FinalResult1 – CurrentRating1) : (FinalResult1 – CurrentRating1));
        Point2 = (((FinalResult2 – CurrentRating2) > 0) ? (FinalResult2 – CurrentRating2) : (FinalResult2 – CurrentRating2));

        can be simplified to:

        Point1 = FinalResult1 – CurrentRating1;
        Point2 = FinalResult2 – CurrentRating2;

        It doesn’t matter if (FinalResult1 – CurrentRating1) or (FinalResult2 – CurrentRating2) is positive or not.

        • luke

          ah good point that’s me being to careful with negative numbers and divide by zero exception (I know no divide to be seen)

  3. Luke Andrews

    How could I use this code sorry for the novice question I
    am new to all this and found the facemash a brilliant concept and
    if we could redevelop it and get a lot of traffic

  4. Terez

    Hi, nice work!
    I have a question though. Is it possible to grow the database of the people over the time? I mean lets say that the facemash is already running a month, then you want to add next 20people to the system. What happens? If I understand it well, then you assign every new person some fixed rating – e.g. 1500. So now we have in a system people, that already have been in numerous duels and have a rating lets say 900 even though they look obvisously better then the new ones. How long will it take before the results of the newly added people become objective? How do you pick profiles that will be taken to the duel? Is it absolutely random, or do you have some kind of a system?
    Thanks in advance for a reply, this is realy an interesting topic for me.
    Terez.

    • luke

      Yes the “duel” is completely random.
      How long it takes depends on how many people you have voting and how many votes they are making.
      You should let the algorithm do the work if you are removing contestants then its not going to be as natural.

  5. Hey man, I made a site similar to yours before I found out about it. Check out mugshotwars and leftright

    You might be interested in my Javascri

    • Xavier

      can you send me the link for your site?

  6. Tony

    Could I wrote this code in the Visual C# program by Microsoft? And if so, how do I publish it into my website?

    • luke

      Yes you’ll need to download Visual Studio 2010 Express, its free!

  7. angelkorn

    Hey Man,

    How can I come up with this problem in voting system using your Elo Rating Class?

    “Image 1 was voted for over Image 2 86% of the time”

    and generate display:

    Top 20 Image:

    Image 1 96% (over image 7)
    Image 4 92% (over image 3)
    Image 3 84% (over image 2)
    etc.

    Please help me…

    Thanks

  8. Xavier

    I’m totally excited about creating a website like this or similar, could you guys help me?

    How could I start? What do I need to know? I also checked your own facemash and in some pages I couldnt click on the images!

    Thanks
    Great site btw.

    • luke

      Sure!
      The most difficult part is on this page.

      What did you need help with exactly?

      • xavier

        what software do i need to learn to build a website like face mash? how did you get the pictures in the first place? im totally excited about this

        • luke

          You can use any code editor you like! It sounds as though you should attempt a smaller project first.
          The pictures are retrieved through the myspace search API.
          Enjoy!

  9. Edwin S

    dude, you saved my life

  10. Xavier

    why you dont answer? is the question too obvious?

Submit a Comment