GiniSim is a simple Javascript program which demonstrates, in simple terms, flaws in free market economics, by showing that trading freely will lead to gross inequality. Copy the program into a .html file; save it and then open it in a browser: you can stop it by pressing the Stop button. Alternatively, you can download and run a simple Java version, GiniSim.jar from here.
Each bar is the wealth of a person, and the simulation starts with everyone having $10 (or £10, or 10€).
<!DOCTYPE html>
<html>
<body>
<button onclick="clearInterval(timer)">Stop</button>
<canvas id="myCanvas" width="800" height="600" style="border:1px solid #c3c3c3;">
Your browser does not support the HTML5 canvas tag.
</canvas>
<script>
var c=document.getElementById("myCanvas");
var ctx=c.getContext("2d");
var timer;
var rects=0;
var cash=[0],people=[0];
var kStartCash=10;
var kNumPeople=100;
function initGini() {
var ix,iy;
for(ix=0;ix<kNumPeople;ix++) {
people[ix]=kStartCash; // everyone starts with $10.
for(iy=0;iy<kStartCash;iy++) {
cash[ix*kStartCash+iy]=ix; // each $1 is owned by a person.
}
}
}
function incomeSwap(from,too) {
var ix;
for(ix=0;ix<kNumPeople*kStartCash;ix++) {
if(cash[ix]==from)
cash[ix]=too;
else if(cash[ix]==too)
cash[ix]=from;
}
ix=people[from];
people[from]=people[too];
people[too]=ix;
}
function exchange() {
var aNote=Math.floor(Math.random()*kStartCash*kNumPeople);
var aOwner=cash[aNote];
var aNewNote=Math.floor(Math.random()*kStartCash*kNumPeople);
var aNewOwner=cash[aNewNote];
if(people[aOwner]>0 ) {
// can't take cash from people who have nothing.
//}while(aNewOwner==aOwner);
people[aOwner]-=1;
people[aNewOwner]+=1;
cash[aNote]=aNewOwner;
while(aOwner>0 && people[aOwner]<people[aOwner-1]) {
incomeSwap(aOwner,aOwner-1);
aOwner--;
if(aOwner==aNewOwner)
aNewOwner++;
}
while(aNewOwner<kNumPeople-1 && people[aNewOwner]>people[aNewOwner+1]) {
incomeSwap(aNewOwner,aNewOwner+1);
aNewOwner++;
}
}
}
function drawImage() {
var ix;
for(ix=0;ix<kNumPeople;ix++) {
ctx.fillStyle="#c0c0c0";
ctx.fillRect(ix*8,0,8,600-people[ix]);
ctx.fillStyle="#000000";
ctx.fillRect(ix*8,600-people[ix],8,600);
}
exchange();
}
initGini();
timer=setInterval(drawImage,1);
</script>
</body>
</html>
[Edit: Added link to GiniSim.jar on 20150321. Edit: I knew from the simulations that wealth always gravitates to the rich, but my reasoning for the mechanism was incorrect, because I was trying to derive it from the probabilities of a single transaction. In reality, the mechanism is due to how the probabilities change between one transaction and the next. 20150626.]