Skip to content

Instantly share code, notes, and snippets.

@ereidland
Created June 23, 2015 01:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ereidland/0ce749d9015f0d7cabe8 to your computer and use it in GitHub Desktop.
Save ereidland/0ce749d9015f0d7cabe8 to your computer and use it in GitHub Desktop.
Rough testing of average stat roll for DnD
function Roll(sides)
{
return 1 + Math.round(Math.random()*(sides - 1));
}
function RollForStat()
{
var totals = [Roll(6), Roll(6), Roll(6), Roll(6)];
var lowestIndex = 0;
var lowestNumber = totals[0];
for (var i = 1; i < totals.length; i++)
{
var currentNumber = totals[i];
if (currentNumber < lowestNumber)
lowestIndex = i;
}
var numberCount = totals.length - 1;
var totalNumber = 0;
for (var i = 0; i < totals.length; i++)
{
if (lowestIndex == i)
continue;
totalNumber += totals[i];
}
return totalNumber;
}
function TestAverageStat(howManyTimes)
{
var totalNumber = 0;
for (var i = 0; i < howManyTimes; i++)
{
var stat = RollForStat();
totalNumber += stat;
}
return totalNumber / howManyTimes;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment