{{ message }}

Instantly share code, notes, and snippets.

# kentbrew/wilensky.js

Created Jul 9, 2017
Imagine a room full of 100 people with 100 dollars each. With every tick of the clock, every person with money gives a dollar to one randomly chosen other person. After some time progresses, how will the money be distributed?
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
 // paste me into console to explore the counterintuitive problem found here: // http://www.decisionsciencenews.com/2017/06/19/counterintuitive-problem-everyone-room-keeps-giving-dollars-random-others-youll-never-guess-happens-next/ // setup: tweak as needed var rounds = 10, turns = 1000, max = 100, start = 100; // play one round var round = function () { // setup var a = []; // everybody gets the same number of things to start for (var i = 0; i < max; i = i + 1) { a.push(start); } // everybody who has at least one thing: give one to somebody else var turn = function () { for (var i = 0; i < max; i = i + 1) { if (a[i]) { a[i] = a[i] - 1; var r = Math.floor(Math.random() * max); a[r]++; } } }; // run some turns for (var t = 0; t < turns; t = t + 1) { turn(); } // sort so it's easy to spot highs and lows a.sort(function (a, b) { return b - a; }); //return results return a; }; // play some rounds and log them to console for (var i = 0; i < rounds; i = i + 1) { console.log(round()); }

### jones1618 commented Jul 9, 2017

Nice. I copied this into a JSFiddle so people could play with it without opening a console.
https://jsfiddle.net/jones1618/n5w5nyo6/