随机数的生成:min到max之间,包括两者自身
parseInt(Math.random()*(max-min+1)+min,10);
Math.floor(Math.random()*(max-min+1)+min);
获取随机数列:常用于打乱出场次序又能整体通过
//获取范围内的随机数列,乱序不重复 function getOrder(start, end){ var len = end - start + 1; var myorder = new Array(); var index = 0; while (index < len) { var flag = true; var num = parseInt(Math.floor(Math.random() * len) + start); for (var i = 0; i < myorder.length; i++) { if (myorder[i] == num) { flag = false; break; } } if (flag) { myorder[index] = num; index++; } } return myorder; }
调用:getOrder(1,10);
返回值示例:[7, 1, 4, 8, 3, 5, 2, 9, 6, 10]、[9, 3, 8, 5, 2, 10, 6, 7, 4, 1]、[1, 5, 7, 10, 2, 4, 3, 8, 9, 6]
数值转金融:给数值以金融货币的形式加逗号
function numFormat(num){ var res=num.toString().replace(/d+/, function(n){ // 先提取整数部分 return n.replace(/(d)(?=(d{3})+$)/g,function($1){ return $1+","; }); }) return res; }
调用:numFormat(1234567);
返回值:'123,456,789'