Return to Snippet

Revision: 24099
at February 21, 2010 03:00 by johncoltrane


Updated Code
var roll = {
  d4 : function() {
    return Math.floor(Math.random() * (4 - 1 + 1)) + 1;
  },
  d6 : function() {
    return Math.floor(Math.random() * (6 - 1 + 1)) + 1;
  },
  d8 : function() {
    return Math.floor(Math.random() * (8 - 1 + 1)) + 1;
  },
  d10 : function() {
    return Math.floor(Math.random() * (10 - 1 + 1)) + 1;
  },
  d12 : function() {
    return Math.floor(Math.random() * (12 - 1 + 1)) + 1;
  },
  d20 : function() {
    return Math.floor(Math.random() * (20 - 1 + 1)) + 1;
  }
};

console.log("rolling D4 : " + roll.d4());
console.log("rolling D20 : " + roll.d20());

Revision: 24098
at February 21, 2010 02:59 by johncoltrane


Initial Code
var roll = {
  d4 : function() {
    return Math.floor(Math.random() * (4 - 1 + 1)) + 1;
  },
  d6 : function() {
    return Math.floor(Math.random() * (6 - 1 + 1)) + 1;
  },
  d8 : function() {
    return Math.floor(Math.random() * (8 - 1 + 1)) + 1;
  },
  d10 : function() {
    return Math.floor(Math.random() * (10 - 1 + 1)) + 1;
  },
  d12 : function() {
    return Math.floor(Math.random() * (12 - 1 + 1)) + 1;
  },
  d20 : function() {
    return Math.floor(Math.random() * (20 - 1 + 1)) + 1;
  }
};

console.log("rolling D4 : " + roll.d4());
console.log("rolling D20 : " + roll.d20());

Initial URL


Initial Description


Initial Title
roll dice in javascript

Initial Tags
javascript, textmate

Initial Language
JavaScript