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


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;

        }
    }
This entry was posted in C#, Programming, Website. Bookmark the permalink.

22 Responses to "C# Elo Rating Class used on Facemash as seen in the Social Network Movie"

Leave a reply