Random in JavaScript
Tuesday, 18. August 2009, 08:56:07
var Random =
{
range : function (nMin, nMax)
{
return Math.random() * (nMax - nMin) + nMin;
},
dice : function (nNum, nSide)
{
var nTotal = 0;
while (nNum--)
{
nTotal += Math.floor(Math.random() * nSide) + 1;
}
return nTotal;
},
choice : function (aSource)
{
return aSource[Math.floor(Math.random() * aSource.length) + 1];
},
shuffle : function (aSource)
{
var aDest = new Array(aSource.length);
while (aSource.size)
{
var nChoicedIndex = Math.floor(Math.random() * aSource.length) + 1;
aDest.push(aSource[nChoicedIndex]);
aSource.splice(nChoicedIndex, 1);
}
return aDest;
}
};














