Javascript

Code modified from that used at http://www.jsdice.com/roller/, which is distributed under the FreeBSD license.

<!--
Copyright 2009 jsdice.com. All rights reserved.
 
Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
 
   1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
   2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
 
THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.    
-->
<html>
<head>
    <title>Dice Roller</title>
<style type="text/css">
  body {background-color: #EEEEEE;}
</style>
    <script type="text/javascript">
    function roll(dice)
    {
        var dice = dice.replace(/- */,'+ -');
        var dice = dice.replace(/D/,'d');
        var re = / *\+ */;
        var items = dice.split(re);
        var res = new Array();
        var type = new Array();
        for ( var i=0; i<items.length; i++) {
            var match = items[i].match(/^[ \t]*(-)?(\d+)?(?:(d)([\dF]+))?[ \t]*$/);
            if (match) {
                var sign = match[1]?-1:1;
                var num = parseInt(match[2] || "1");
                var max = parseInt(match[4] || "0");
                if (match[3]) {
                    for ( j=1; j<=num; j++) {
                        var t;
                        if (match[4]=="F") {
                            t = sign * Math.ceil(3*random(seed)-2);
                            max = "F";
                        } else {
                            t = sign * Math.ceil(max*random(seed));
                        }
                        res[res.length] = t;
                          seed = seed * seeds[1];
                        type[type.length] = max;
                    }
                }
                else {
                    res[res.length] = sign * num;
                    type[type.length] = 0;
                }
            } 
            else return null;
        }
        if (res.length == 0) return null;
        return {"res":res, "type":type};
    }
 
    function resultStr(data)
    {
        var total = 0;
        var str = "";
        for (var i=0; i<data.res.length; i++) {
            total = total + data.res[i];
            if (data.res.length>1) {
                if (i) str = str + ((data.res[i])>=0?"+ ":"- ");
                str = str + Math.abs(data.res[i]);
                if (data.type[i]) str = str + "<sub><em>d"+data.type[i]+"</em></sub>"
                else if (i<data.res.length-1) str = str + " ";
            }
        }
        str = "<strong>" + total + "</strong>" + (str?"&nbsp;(" + str + ")":'');
        return str;
    }
 
    function rollDice(dice)
    {
        if (!dice) return;
 
        var data = roll(dice); var str = "<em>" + dice + "</em> = ";
        if (data) {
            str += resultStr(data);
        } else {
            str += "[<strong>Error in the roll formula</strong>]";
        }
            document.write('<div class="roll_result">' + str + '</div>');
    }
 
function random(seed) {
  seed = (seed*9301+49297) % 233280;
  return seed/(233280.0);
}
    </script>
</head>
<body>
<script type="text/javascript">
var seeds = unescape(window.location.hash).split("").reverse().join("").split("#");
for (var i=0; i<seeds.length; i++) {
  seeds[i] = seeds[i].split("").reverse().join("");
}
var seed = seeds[0]*seeds[1];
 
var dice;
var ds = seeds[seeds.length-2].split(/;\s*/);
for (var i=0; i<ds.length; i++) {
  rollDice(ds[i]);
}
</script>
</body>
</html>
Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License