Attribute Rolling (Into Pointbuy)

Attribute Rolling (Into Pointbuy)

by XarX

HTML

<pre id=painted></pre>

JavaScript

var _painted = 0, $painted = document.getElementById('painted');

var runs = 1000000,
attributes = 6,
points = 0,
rolledNumbers = [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0];


for (run=1;run<=runs;run++) {
	var stat = new Array();
	for (i=0;i<attributes;i++) stat[i] = 0;
	for (attribute=0; attribute<attributes; attribute++) {
  	stat[attribute] = roll4d6k3();
    rolledNumbers[stat[attribute]]++;
    points+= convertStatToPoints(stat[attribute])/runs;
  }
}

$painted.textContent+= "Average Points: " + Math.round(100*points)/100 + "\n\n";

for (i=0;i<20;i++) $painted.textContent+= ((i<10)?"0":"") + i + " -> " + Math.round(10000*rolledNumbers[i]/(runs*attributes))/10000 + "\n"; 
function roll4d6k3() {
	var smallest = 6;
  var output = 0;
	for (i=1;i<=4;i++) {
  	var temp = rollRandom(1,6);
  	output+= temp;
    //$painted.textContent+= output + " (+" + temp + ")\n";
    if (temp < smallest) smallest = temp;
  }
  output-=smallest;
  //$painted.textContent+= output + " ( Smallest: " + smallest + ")";
  return output;
}

function convertStatToPoints(x) {
	if (x < 3) $painted.textContent+= "Err (Stat too low)";
	else if (x <= 13) return (x-8);
  else if (x > 13 && x <= 16) return ((x-14)*2+7);
  else if (x > 16 && x <= 18) return ((x-17)*3+14);
  else if (x > 18) $painted.textContent+= "Err (Stat too high)";
  else $painted.textContent+= "Err (No Integer Input)";
}

function rollRandom(min, max) {
  min = Math.ceil(min);
  max = Math.floor(max);
  return Math.floor(Math.random() * (max - min +1)) + min; 
}  

$painted.textContent+= "\n Done!";