Home
Bio
Fridge
Goldfish
Blog
Patriot Savant
Grumble Magazine
Bray New World
Sounding Board
Folded

Dice Roller JavaScript

More than once I've written pages where I need script to, in essence, roll a bunch of dice. Ye fellow gamer geeks will empathize. So I wrote this script and stuck it here so I'd have it handy.

It uses the substr() method, which shouldn't be a problem unless you're somehow reading this page in 1998.

/*
roll();
Author: Jeff Yaus

A dice roll simulator. 

roll(x,y)
  Rolls x dice, each having y sides, and returns the result.
  So roll(3,6) rolls 3d6

roll("xdy")
  Rolls x dice, each having y sides, and returns the result.
  Note: quotes are critical.
  So roll("3d6") rolls 3d6
 
roll(z)
  Rolls 1 die of z sides, and returns the result.
  So roll(20) rolls 1d20

*/

function roll() {
  var numberOfDice = 0;
  var numberOfSides = 0;
  var total = 0;

  if (arguments.length==2) {
    numberOfDice = arguments[0];
    numberOfSides = arguments[1];
  } else if (arguments.length==1) {
    var arg = arguments[0] + "";
    if (arg.indexOf("d") > 0)  {
      numberOfDice = arg.substring(0,arg.indexOf("d"));
      numberOfSides = arg.substr(arg.indexOf("d")+1);
    } else {
      numberOfDice = 1;
      numberOfSides = arguments[0];
    }
  }

  for (i=numberOfDice; i>0; i--) {
    total += Math.floor(Math.random()*numberOfSides) + 1;
  }

  return total;
}


(Why does the for loop decrement instead of increment? Because this page is pasting in the script from elsewhere, and a < symbol looks like the beginning of an HTML tag to your browser. So we decrement and use > instead.)