This commit is contained in:
dengqichen 2025-11-21 18:35:34 +08:00
parent 3308b7d1d0
commit 1382ebd104

View File

@ -143,12 +143,12 @@ export class CardGeneratorTool extends BaseTool<CardGeneratorConfig> {
const rand = Math.random(); const rand = Math.random();
if (rand < 0.5) { if (rand < 0.5) {
// 50% 权重生成严格按照70张成功案例的统计规律 // 50% 马尔可夫链生成(考虑位置关联性
return this.generateByWeights(selectedPrefix, digitsNeeded, length); return this.generateByWeights(selectedPrefix, digitsNeeded, length);
} }
if (rand < 0.9) { if (rand < 0.9) {
// 40% 变异生成从成功案例变异1-2位 // 40% 变异生成从成功案例变异1-2位
return this.generateByMutation(selectedPrefix, successfulPatterns || [], generation.mutationDigits || [1, 1], digitsNeeded, length); return this.generateByMutation(selectedPrefix, successfulPatterns || [], generation?.mutationDigits || [1, 1], digitsNeeded, length);
} }
// 10% 纯随机(保持多样性) // 10% 纯随机(保持多样性)
} }
@ -280,7 +280,7 @@ export class CardGeneratorTool extends BaseTool<CardGeneratorConfig> {
} }
/** /**
* 60 *
* @param {string} prefix - BIN前缀 * @param {string} prefix - BIN前缀
* @param {number} digitsNeeded - * @param {number} digitsNeeded -
* @param {number} totalLength - * @param {number} totalLength -
@ -291,23 +291,33 @@ export class CardGeneratorTool extends BaseTool<CardGeneratorConfig> {
return generateLuhnNumber(prefix, totalLength); return generateLuhnNumber(prefix, totalLength);
} }
// 基于70个真实成功案例的位置权重后2位不含校验位 if (digitsNeeded !== 2) {
const positionWeights = [ // 如果不是2位降级到纯随机
[12, 7, 10, 7, 8, 5, 5, 9, 6, 1], // 百位: 0占17.1%
[9, 7, 8, 3, 6, 7, 3, 10, 9, 8] // 十位: 7占14.3%
];
if (digitsNeeded > positionWeights.length) {
return generateLuhnNumber(prefix, totalLength); return generateLuhnNumber(prefix, totalLength);
} }
let pattern = ''; // 第1位百位的权重分布
for (let pos = 0; pos < digitsNeeded; pos++) { const firstDigitWeights = [12, 7, 10, 7, 8, 5, 5, 9, 6, 1];
const weights = positionWeights[pos]; const firstDigit = this.weightedRandomDigit(firstDigitWeights);
const digit = this.weightedRandomDigit(weights);
pattern += digit;
}
// 第2位基于第1位的马尔可夫转移概率
const markovTransitions: Record<string, number[]> = {
'0': [3, 1, 1, 2, 0, 2, 1, 2, 0, 0],
'1': [0, 0, 2, 0, 1, 2, 0, 2, 0, 0],
'2': [2, 0, 0, 0, 1, 2, 0, 1, 4, 0],
'3': [0, 2, 2, 0, 0, 0, 1, 2, 0, 0],
'4': [1, 0, 1, 0, 3, 0, 0, 1, 1, 1],
'5': [0, 3, 1, 0, 0, 0, 0, 0, 1, 0],
'6': [1, 0, 0, 0, 1, 0, 0, 1, 1, 1],
'7': [1, 1, 1, 1, 0, 0, 1, 0, 2, 2],
'8': [1, 0, 0, 0, 0, 1, 0, 1, 0, 3],
'9': [0, 0, 0, 0, 0, 0, 0, 0, 0, 1]
};
const secondDigitWeights = markovTransitions[firstDigit] || [1, 1, 1, 1, 1, 1, 1, 1, 1, 1];
const secondDigit = this.weightedRandomDigit(secondDigitWeights);
const pattern = firstDigit + secondDigit;
const partial = prefix + pattern; const partial = prefix + pattern;
const checkDigit = this.calculateLuhnCheckDigit(partial); const checkDigit = this.calculateLuhnCheckDigit(partial);