Files
it-tools/src/tools/pin-code-generator/pin-code-generator.service.ts
T
2024-08-24 11:15:14 +02:00

23 lines
495 B
TypeScript

export function randomNumber({
length,
repeatDigits = false,
}: {
length: number
repeatDigits?: boolean
}) {
const arr = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9];
const result: number[] = [];
const willRepeat = length > 10 || repeatDigits;
while (result.length < length) {
const random = Math.floor(Math.random() * 10); // NOSONAR
const num = arr[random];
if (!willRepeat && result.includes(num)) {
continue;
}
result.push(num);
}
return result.join('');
}