共计 595 个字符,预计需要花费 2 分钟才能阅读完成。
上面的三个办法我之前都用到过,面向根本的数据处理,可组合性较高:
// 返回指定范畴内的随机整数(不蕴含 _end)function GetRandomInt(_start, _end) { | |
var x = _end - _start; | |
var num = Math.random() * x + _start; | |
return parseInt(num, 10); | |
} | |
// 返回指定范畴内的随机数(不蕴含 _end)function GetRandom(_start, _end) { | |
var x = _end - _start; | |
var num = Math.random() * x + _start; | |
return num; | |
} | |
// 返回指定长度的随机英文字母 | |
function GetRandomLetters(_length) { | |
var str = ""; | |
for (var i = 0; i < _length; i++) {if (parseInt(Math.random() * 2, 10)) {var num = Math.random() * 26 + 65; | |
str += String.fromCharCode(num); | |
} else {var num = Math.random() * 26 + 97; | |
str += String.fromCharCode(num); | |
} | |
} | |
return str; | |
} |
比方 GetRandomInt(0,2) 可能在 0 和 1 之间随机返回一个,GetRandomLetters(5) 可能获取 5 个随机英文字母(包含大小写)。
正文完
发表至: javascript
2021-04-16