[AS和JS]随机关键字生成方法(AS和JS里都可以使用)
2012-02-06 12:14:39 来源:本站整理核心提示:随机关键字生成方法(AS和JS里都可以使用)function getRandomKeyID(len){ if (len == undefined) { len = 6; } var keyID = ""; var char_arr = new Array(); char_arr.push
随机关键字生成方法(AS和JS里都可以使用)
- function getRandomKeyID(len){
- if (len == undefined) {
- len = 6;
- }
- var keyID = "";
- var char_arr = new Array();
- char_arr.push(function () {
- return String.fromCharCode(48+Math.floor(Math.random()*10));
- });
- char_arr.push(function () {
- return String.fromCharCode(97+Math.floor(Math.random()*26));
- });
- char_arr.push(function () {
- return String.fromCharCode(65+Math.floor(Math.random()*26));
- });
- for (var i = 0; i<len; i++) {
- keyID += char_arr[Math.floor(Math.random()*char_arr.length)]();
- }
- return keyID;
- }
//获取在两个数值之间(包含两个数值)的随机数方法(AS和JS里都可以使用)
- function getRandomNumber(minNum,maxNum){
- if(minNum == undefined){
- minNum = 0;
- maxNum = 10;
- }else if(maxNum == undefined){
- maxNum = minNum;
- minNum = 0;
- }else if(maxNum < minNum){
- var tmpNum = maxNum;
- maxNum = minNum;
- minNum = tmpNum;
- }
- return Math.floor(Math.random()*(maxNum-minNum+1))+minNum;
- }
//全字符串替换(AS和JS里都可以使用)
- function replaceAll(str,oldStr,reStr){
- return str.split(oldStr).join(reStr);
- }
更多精彩
赞助商链接