Skip to content

Instantly share code, notes, and snippets.

@torjusb
Created June 27, 2011 16:20
Show Gist options
  • Save torjusb/1049202 to your computer and use it in GitHub Desktop.
Save torjusb/1049202 to your computer and use it in GitHub Desktop.
Simulate raid healing attendance
var fs = require('fs'),
input = {
nifty: 91,
shaggus: 92,
svansemanden: 100,
plencis: 90,
turnunder: 75,
moui: 79,
mayoto: 85,
galacta: 77,
shalelol: 95,
ozma: 74
}, output = [], i;
function simulateRaid () {
var output = {
numAttending: 0,
data: []
},
name, attendance,
chance, willAttend;
for (name in input) {
attendance = input[name];
chance = Math.ceil(Math.random() * 100);
willAttend = !(chance > attendance);
output.data.push({
name: name,
attending: willAttend
});
willAttend && output.numAttending++;
}
return output;
}
function getStats (data) {
var total = 0, min, max, i = 0,
num;
for (; i < data.length; i++) {
num = data[i].numAttending;
total += num;
if (!max && !min) {
max = min = num;
} else {
max = Math.max(num, max);
min = Math.min(num, min);
}
}
return {
avrage: Math.floor( total / data.length ),
min: min,
max: max
};
}
function formatOutput (data) {
var output = "", i = 0, row;
output += "Raid;Num Attending\n";
for (; i < data.length; i++) {
row = data[i];
output += "Raid " + i + ";" + row.numAttending + "\n";
}
output += " ; \n";
output += "Min raiders;" + data.stats.min + "\n";
output += "Max raiders;" + data.stats.max + "\n";
output += "Total avrage;" + data.stats.avrage;
return output;
}
for (i = 0; i < 100; i++) {
output.push( simulateRaid() );
}
output.stats = getStats(output);
fs.open('./output.csv', 'w+', '0666', function (err, fd) {
fs.writeSync(fd, formatOutput(output), 0);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment