如何使用Javascript中的以下条件生成随机数
注意:由于遗传原因,我不想使用任何JS库
我尝试了下面的代码,但它不符合上述条件,例如有时它根本不包含数字....
function randomString(length, chars) {
var mask = '';
if (chars.indexOf('a') > -1) mask += 'abcdefghijklmnopqrstuvwxyz';
if (chars.indexOf('A') > -1) mask += 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
if (chars.indexOf('#') > -1) mask += '0123456789';
var result = '';
for (var i = length; i > 0; --i) result += mask[Math.round(Math.random() * (mask.length - 1))];
return result;
}
document.write(randomString(12, 'aA#'));
有没有更好的方法呢?
答案 0 :(得分:2)
以下是如何做到这一点:生成一个代表你的标准的数组,将其洗牌并填充数组。
示例:
function makeRandomString(criteria) {
// From http://stackoverflow.com/q/2450954/3371119
function shuffle(array) {
var currentIndex = array.length,
temporaryValue, randomIndex;
// While there remain elements to shuffle...
while (0 !== currentIndex) {
// Pick a remaining element...
randomIndex = Math.floor(Math.random() * currentIndex);
currentIndex -= 1;
// And swap it with the current element.
temporaryValue = array[currentIndex];
array[currentIndex] = array[randomIndex];
array[randomIndex] = temporaryValue;
}
return array;
}
// Choose a random character from a string
function chooseRandom(str) {
return str[Math.floor(Math.random() * str.length)];
}
// Shuffle the criteria
shuffle(criteria);
var result = "";
// Build the resulting string by choosing a random character from each part
for (var i = 0; i < criteria.length; ++i) result += chooseRandom(criteria[i]);
return result;
}
使用示例:
// Some constants explaining the criteria
var lowercase = "abcdefghijklmnopqrstuvwxyz";
var uppercase = lowercase.toUpperCase();
var numbers = "0123456789";
// Note: if you don't like typing all that, change the names to L, N, and U
var criteria = [lowercase, lowercase, lowercase, lowercase, // 4 lowercase
numbers, numbers, numbers, numbers, // 4 numbers
uppercase, uppercase, uppercase, uppercase // 4 uppercase
];
console.log(makeRandomString(criteria));
甚至更好(更少打字):
function repeat(elem, n) {
var result = [];
for (var i = 0; i < n; ++i) result.push(elem);
return result;
}
var criteria = repeat(lowercase, 4)
.concat(repeat(uppercase, 4))
.concat(repeat(numbers, 4));
console.log(makeRandomString(criteria));
答案 1 :(得分:0)
这是我的解决方案:
var text = " ";
var numChars = "0123456789";
var upCaseChars="ABCDEFGHIJKLMNOPQRSTUVWXYZ";
var lowCaseChars="abcdefghijklmnopqrstuvwxyz";
for( var i=0; i < 4; i++ )
{
text += numChars.charAt(Math.floor(Math.random() * numChars.length));
text += upCaseChars.charAt(Math.floor(Math.random() * upCaseChars.length));
text += lowCaseChars.charAt(Math.floor(Math.random() * lowCaseChars.length));
}
console.log(text);
我认为它应该可以解决问题,你可以测试它here。
答案 2 :(得分:0)
遵循同样的理念,你可以尝试这样的事情。您将生成一个随机字符串,每个字符串至少包含4个字符(因此至少12个字符),最多为您指定为参数的字符数。
var LOWER_CASE_MASK = 'abcdefghijklmnopqrstuvwxyz';
var UPPER_CASE_MASK = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
var NUMBER_MASK = '0123456789';
var ALL_MASK = LOWER_CASE_MASK + UPPER_CASE_MASK + NUMBER_MASK;
function randomString(length) {
var result = '';
for (var i = 0; i < 4; i++) {
result += randomise(LOWER_CASE_MASK);
result += randomise(UPPER_CASE_MASK);
result += randomise(NUMBER_MASK);
}
for (var j = 0; j < length - 12; j++) {
result += ALL_MASK[Math.round(Math.random() * (ALL_MASK.length - 1))];
}
return shuffle(result);
}
function randomise(string) {
return string[Math.round(Math.random() * (string.length - 1))];
}
function shuffle(string) {
var parts = string.split('');
for (var i = parts.length; i > 0;) {
var random = parseInt(Math.random() * i);
var temp = parts[--i];
parts[i] = parts[random];
parts[random] = temp;
}
return parts.join('');
}
document.write(randomString(20));
shuffle函数是Fisher-Yates shuffle的实现。
Plunker here。
希望这有帮助。