一、生成从minNum到maxNum的随机数(区间内的两位小数随机数)
function randomNum(minNum, maxNum) { switch (arguments.length) { case 1: return parseFloat(Math.random() * minNum + 1).toFixed(2); break; case 2: return parseFloat(Math.random() * (maxNum - minNum + 1) + minNum).toFixed(2); break; default: return 0; break; } }
二、按照现有名称,随机显示:
//名称数组 var store_name = ["脚链", "发夹", "胸针", "毛衣链", "戒指", "手链", "项链", "耳环", "捷克钻", "奥地利钻"]; //调用名称; store_name[Math.floor((Math.random() * store_name.length))]
三、随机时间的自动生成
/*随机时间 -Begin*/ function RandomNumBoth(Min, Max) { var Range = Max - Min; var Rand = Math.random(); var num = Min + Math.round(Rand * Range); if (num <= 9) { num = '0' + num; } return num; } //分秒自动补足0; function timeAdd0(str) { if (str.length <= 1) { str = '0' + str; } return str } var nowYear = new Date().getFullYear().toString() var nowMonth = (new Date().getMonth() + 1).toString() var nowDay = new Date().getDate().toString(); var nowHours = new Date().getHours().toString(); var nowMin = new Date().getMinutes().toString(); nowYear = timeAdd0(nowYear) nowMonth = timeAdd0(nowMonth) nowDay = timeAdd0(nowDay) nowHours = timeAdd0(nowHours) nowMin = timeAdd0(nowMin) /*随机时间 -End*/
Done!