内置 jQuery 扩展,但不允许最大 return 值
Built jQuery extension, but not allowing max return values
我在这里整理了一个问题的 jsfiddle:https://jsfiddle.net/sd1x3am3/5/
基本上,我构建了一个名为 $.generateSerial
的扩展,它有 3 个参数。第一个参数是公式,第二个是允许的字符,第三个是不包含在 return 值中的序列号。基本上,该扩展允许您生成一个独特的随机序列,即第 3 个参数中序列的 none,以及在 [=15] 中 return 的序列的 none =] 变量每次 $.generateSerial
在 for
循环中被调用。
由于某些原因,$.generateSerial
returns ""
在 i = 146
的循环中,但它应该有更多的值 return,所以它不应该 return ""
.
公式是XXXX-XXXX-XXXX-XXXX
,允许的字符是:ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789
,这意味着这些字符中的任何一个都可以替换formula
中的任何X
个字符。但是应该有超过 145/146 个结果 returned,不计算 currSerials
数组中的结果。应该可以生成更多连续剧。
但我不明白为什么它没有生成所有可能的连续剧并停止...
$.extend({
generateSerial: function(formula, chrs, checks) {
var formula = formula && formula != "" ? formula : 'XXXXX-XXXXX-XXXXX-XXXXX-XXXXX', // Default Formula to use, should change to what's most commonly used!
chrs = chrs && chrs != "" ? chrs : "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789", // Default characters to randomize, if not defined!
len = (formula.match(/X/ig) || []).length,
oStatic = formula.match(/[^X]+/ig) || [],
indices = [],
lenChecks = checks.length || 0,
rand;
// Let's first find out if it's possible to create another random serial from params
// Should put the formula below through a stress test here to be sure it works!
var possible = (len * chrs.length) - lenChecks;
// return empty string if not possible!
if (possible <= 0)
return '';
var currIndex = 0;
// Getting all indices here of all strings that are not X or x
for (var o = 0; o < oStatic.length; o++) {
currIndex = formula.indexOf(oStatic[o], currIndex);
indices.push(currIndex);
currIndex = currIndex + oStatic[o].length; // set the position to start at for next loop! incrementing...
}
do {
rand = Array(len).join().split(',').map(function() {
return chrs.charAt(Math.floor(Math.random() * chrs.length));
}).join('');
// Rebuild with indices, if exists!
if (indices && indices.length > 0) {
for (var x = 0; x < indices.length; x++)
rand = rand.insert(indices[x], oStatic[x]);
}
} while (checks && $.inArray(rand, checks) !== -1);
return rand;
}
});
有人可以帮我解决这个问题吗?扩展中的 possible
变量可能有问题吗?不对吗?
甚至在没有 currSerials
数组的情况下尝试过它,它最初是空的并且只生成了 576 个序列,然后它说它不能再生成了。你可以在这里看到它:https://jsfiddle.net/sd1x3am3/8/
var currSerials = [];
for (var i = 0; i < 2000; i++) {
var output = $.generateSerial('XXXX-XXXX-XXXX-XXXX', 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789', currSerials);
$(".serials").append($("<p />").text((i+1) + '. ' + output));
if (output != "")
currSerials.push(output);
else
{
alert('Can not generate anymore serials, stopped at ' + i + '.');
break;
}
}
从讨论中,我发现您不是在排列之后,因此您应该使用以下内容来查找 total possible items、
var possible=Math.pow(chrs.length,len) - lenChecks
我在这里整理了一个问题的 jsfiddle:https://jsfiddle.net/sd1x3am3/5/
基本上,我构建了一个名为 $.generateSerial
的扩展,它有 3 个参数。第一个参数是公式,第二个是允许的字符,第三个是不包含在 return 值中的序列号。基本上,该扩展允许您生成一个独特的随机序列,即第 3 个参数中序列的 none,以及在 [=15] 中 return 的序列的 none =] 变量每次 $.generateSerial
在 for
循环中被调用。
由于某些原因,$.generateSerial
returns ""
在 i = 146
的循环中,但它应该有更多的值 return,所以它不应该 return ""
.
公式是XXXX-XXXX-XXXX-XXXX
,允许的字符是:ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789
,这意味着这些字符中的任何一个都可以替换formula
中的任何X
个字符。但是应该有超过 145/146 个结果 returned,不计算 currSerials
数组中的结果。应该可以生成更多连续剧。
但我不明白为什么它没有生成所有可能的连续剧并停止...
$.extend({
generateSerial: function(formula, chrs, checks) {
var formula = formula && formula != "" ? formula : 'XXXXX-XXXXX-XXXXX-XXXXX-XXXXX', // Default Formula to use, should change to what's most commonly used!
chrs = chrs && chrs != "" ? chrs : "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789", // Default characters to randomize, if not defined!
len = (formula.match(/X/ig) || []).length,
oStatic = formula.match(/[^X]+/ig) || [],
indices = [],
lenChecks = checks.length || 0,
rand;
// Let's first find out if it's possible to create another random serial from params
// Should put the formula below through a stress test here to be sure it works!
var possible = (len * chrs.length) - lenChecks;
// return empty string if not possible!
if (possible <= 0)
return '';
var currIndex = 0;
// Getting all indices here of all strings that are not X or x
for (var o = 0; o < oStatic.length; o++) {
currIndex = formula.indexOf(oStatic[o], currIndex);
indices.push(currIndex);
currIndex = currIndex + oStatic[o].length; // set the position to start at for next loop! incrementing...
}
do {
rand = Array(len).join().split(',').map(function() {
return chrs.charAt(Math.floor(Math.random() * chrs.length));
}).join('');
// Rebuild with indices, if exists!
if (indices && indices.length > 0) {
for (var x = 0; x < indices.length; x++)
rand = rand.insert(indices[x], oStatic[x]);
}
} while (checks && $.inArray(rand, checks) !== -1);
return rand;
}
});
有人可以帮我解决这个问题吗?扩展中的 possible
变量可能有问题吗?不对吗?
甚至在没有 currSerials
数组的情况下尝试过它,它最初是空的并且只生成了 576 个序列,然后它说它不能再生成了。你可以在这里看到它:https://jsfiddle.net/sd1x3am3/8/
var currSerials = [];
for (var i = 0; i < 2000; i++) {
var output = $.generateSerial('XXXX-XXXX-XXXX-XXXX', 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789', currSerials);
$(".serials").append($("<p />").text((i+1) + '. ' + output));
if (output != "")
currSerials.push(output);
else
{
alert('Can not generate anymore serials, stopped at ' + i + '.');
break;
}
}
从讨论中,我发现您不是在排列之后,因此您应该使用以下内容来查找 total possible items、
var possible=Math.pow(chrs.length,len) - lenChecks