Election Fraud Simulator Code

Orange RibbonBack to InnoVote

The following JavaScript functions were used in the online election fraud simulator. I should make two points about this:

  1. Most, if not all, actual tabulators would be written in a different programming language.
  2. Any fraud written into the code would be hidden. It certainly wouldn't use the term "fraud" anywhere in it.

However, this is for "educational purposes."


This function runs the other functions, operating in normal mode or election-stealing mode depending on the parameter.

function runSimulation(doFraud)
{
	var myPrecincts = InitializePrecincts();
	if (doFraud == 1)
	{
		OutputResults(myPrecincts, Fraudulate(myPrecincts));
	}
	else
	{
		OutputResults(myPrecincts, Tabulate(myPrecincts));
	}
}
		

This function stores all the necessary attributes of a precinct. Note: This is a very simplistic model. An actual tabulator that used a relational database would, I hope, have a better design. I have a better design, in fact.

function Precinct(name, regvot, totvot, bvote, kvote, tpvote)
{
	this.PrecinctName = name;
	this.RegisteredVoters = regvot;
	this.TotalVotes = totvot;
	this.BushVotes = bvote;
	this.KerryVotes = kvote;
	this.ThirdPartyVotes = tpvote;
	this.BushPercentages = bvote/totvot;
	this.KerryPercentages = kvote/totvot;
	this.Winner;
	if (this.BushVotes > this.KerryVotes)
		this.Winner = "Bush";
	if (this.KerryVotes > this.BushVotes)
		this.Winner = "Kerry";
	if (this.BushVotes == this.KerryVotes)
		this.Winner = "Tie";
}
		

This function initializes a very short list of all precincts in... Ohio, Florida, or New Mexico, maybe.

function InitializePrecincts()
{
	var myPrecincts = new Array(
	new Precinct("A1", 124, 80, 38, 40, 2),
	new Precinct("A2", 551, 313, 166, 142, 5),
	new Precinct("A3", 241, 174, 73, 101, 0),
	new Precinct("A4", 638, 380, 198, 180, 2),
	new Precinct("B1", 279, 160, 62, 98, 0),
	new Precinct("B2", 210, 176, 100, 72, 4),
	new Precinct("B3", 336, 200, 85, 112, 3),
	new Precinct("B4", 177, 105, 56, 48, 1),
	new Precinct("C1", 98, 75, 36, 39, 0),
	new Precinct("C2", 430, 287, 138, 146, 3),
	new Precinct("C3", 320, 196, 97, 97, 2),
	new Precinct("C4", 396, 264, 104, 154, 6));

	return myPrecincts;
}
		

This function initializes a set of results.

function ResultSet()
{
	this.BushTotal = 0;
	this.KerryTotal = 0;
	this.TPTotal = 0;
	this.KerryPercent;
	this.BushPercent;
	this.RegVotersTotal = 0;
	this.TurnoutTotal = 0;
	this.WinnerOverall;
}
		

This section will compute totals and declare a winner based on the ACTUAL votes.

function Tabulate(myPrecincts)
{
	var MyResultSet = new ResultSet();
	for (var i = 0; i < myPrecincts.length; i++)
	{
		MyResultSet.BushTotal += myPrecincts[i].BushVotes;
		MyResultSet.KerryTotal += myPrecincts[i].KerryVotes;
		MyResultSet.TPTotal += myPrecincts[i].ThirdPartyVotes;
		MyResultSet.RegVotersTotal += myPrecincts[i].RegisteredVoters;
		MyResultSet.TurnoutTotal += myPrecincts[i].TotalVotes;
	}

	if (MyResultSet.BushTotal > MyResultSet.KerryTotal)
		MyResultSet.WinnerOverall = "Bush";
	if (MyResultSet.KerryTotal > MyResultSet.BushTotal)
		MyResultSet.WinnerOverall = "Kerry";
	if (MyResultSet.KerryTotal == MyResultSet.BushTotal)
		MyResultSet.WinnerOverall = "Tie";

	MyResultSet.BushPercent = MyResultSet.BushTotal / MyResultSet.TurnoutTotal;
	MyResultSet.KerryPercent = MyResultSet.KerryTotal / MyResultSet.TurnoutTotal;

	return MyResultSet;
}
		

This is a function to compare rankings of precincts. It will be useful later!

function ranking(p1, p2)
{
	return (p1.BushPercentages - p2.BushPercentages);
}
		

Here we begin the election FRAUD simulation! This function will force a candidate (here, Bush) to win by manipulating votes.

function Fraudulate(myPrecincts)
{

Here we check to ensure that the candidate of choice has lost and the race is 5 percentage points or less apart. Otherwise the fraud would be too evident.

var MyResultSet = new ResultSet();

MyResultSet = Tabulate(myPrecincts);

if ( MyResultSet.WinnerOverall != "Bush" && 
    (MyResultSet.KerryPercent - MyResultSet.BushPercent) <= 0.05)
{
	var TargetPercentage = 0.51;
		

We give the candidate a comfortable "lead" of 51% rather than having him win by a few votes, which would trigger a recount -- a hand recount in some states -- and expose the fraud.

var TargetVotes = Math.ceil((TargetPercentage * MyResultSet.TurnoutTotal));
		

This is the actual vote-switching algorithm. First, we reorder all the arrays for ease:

myPrecincts.sort( ranking );
		

Calculate the number of votes to be switched:

var switchVotes = TargetVotes - Math.min (MyResultSet.BushTotal, 
	MyResultSet.KerryTotal);
		

Determine which precincts to mess with (we want to use precincts that the candidate won):

var ManipulatableVotes = 0;

for (var i = 0; i < myPrecincts.length; i++)
{
	if (myPrecincts[i].Winner == "Bush")
		ManipulatableVotes += myPrecincts[i].KerryVotes;
}
		

We're only switching votes for the opponent!

Here we determine the percentage of Kerry votes in Bush precincts to switch.

var switchPercent = switchVotes / ManipulatableVotes;
		

This loop will do the vote-switching.

for (var i = 0; i < myPrecincts.length; i++)
{
		

Here we calculate a variable for the actual votes that will be swapped.

var swap = 0;
		

This conditional takes the votes away from the true winner and gives them to the program's winner.

if (myPrecincts[i].Winner == "Bush")
	{
	swap = Math.round( myPrecincts[i].KerryVotes * switchPercent, 0);
	myPrecincts[i].KerryVotes = myPrecincts[i].KerryVotes - swap;
	myPrecincts[i].BushVotes = myPrecincts[i].BushVotes + swap;
	myPrecincts[i].BushPercentages = 
		myPrecincts[i].BushVotes / myPrecincts[i].TotalVotes;
	myPrecincts[i].KerryPercentages = 
		myPrecincts[i].KerryVotes / myPrecincts[i].TotalVotes;		
	}
}
		

The vote-swapping has been completed; now we can retabulate the tweaked votes.

MyResultSet = Tabulate(myPrecincts);

return MyResultSet;

}
}
		

This function will print the results of a tabulation or "fraudulation."

function OutputResults(myPrecincts, MyResultSet)
{
	document.write("<HTML>\n<head>\n<title>Election Results</title>\n");
	document.write("<link rel=\"stylesheet\" href=\"../../styles.css\" 
		type=\"text/css\">\n</head>\n");
	document.write("<body>\n");
	document.write("<h1>Election Results</h1>\n");

	results = "<table border=1>\n<th>Precinct Name<th>Registered Voters<th>Turnout"
		+ "<th>Bush Total<th>Kerry Total<th>Third-Party Total<th>Bush %<th>
		Kerry %<th>Winner</th>\n";

	for (var i = 0; i < myPrecincts.length; i++)
	{
		results += "<tr><td>" + myPrecincts[i].PrecinctName + 
			"</td><td>" + myPrecincts[i].RegisteredVoters + "</td>" +
			"<td>" + myPrecincts[i].TotalVotes + "</td>" + 
			"<td>" + myPrecincts[i].BushVotes + "</td>" + 
			"<td>" + myPrecincts[i].KerryVotes + "</td>" + 
			"<td>" + myPrecincts[i].ThirdPartyVotes + "</td>" + 
			"<td>" + myPrecincts[i].BushPercentages + "</td>" + 
			"<td>" + myPrecincts[i].KerryPercentages + "</td>" + 
			"<td>" + myPrecincts[i].Winner + "</td></tr>\n";
	}

	results += "<tr><td>Totals</td><td>" + 
			MyResultSet.RegVotersTotal + "</td><td>" + 
			MyResultSet.TurnoutTotal + "</td><td>" + 
			MyResultSet.BushTotal + "</td><td>" + 
			MyResultSet.KerryTotal + "</td><td>" + 
			MyResultSet.TPTotal + "</td><td>" + 
			MyResultSet.BushPercent + "</td><td>" + 
			MyResultSet.KerryPercent + "</td><td>" + 
			MyResultSet.WinnerOverall + "</td></tr>\n";

			results += "</table>\n";

	document.write(results);

	document.write("<br><br><a href=\"simulator.htm\">Return to Simulator</a>");
	document.write("</body>\n</HTML>");

	return 0;
};
		

Any questions should be e-mailed to me.