I'm creating a function that generate a random Uniq Serial id by replacing a string with this format ; xxxx-xxxx-xxxx-xxxx , the goal is to get a serial like that : ABCD-1234-EFGH-5678 ,the first and third parts are a letters and the second and last parts are numbers, this is my code :
public generateUniqSerial() {
return 'xxxx-xxxx-xxx-xxxx'.replace(/[x]/g, function (c) {
let r = Math.random() * 16 | 0, v = c == 'x' ? r : (r & 0x3 | 0x8)
return v.toString(16)
})
}
it give a result like that : "7f8f-0d9a-fd5-450f" , how to edit this function to get a result with this format : ABCD-1234-EFGH-6789 ?
You can do something like this to generate a random Uniq Serial id with a format like ABCD-1234-EFGH-5678
:
function rnd(t) {
let str = '';
const min = t === 'a' ? 10 : 0;
const max = t === 'n' ? 10 : 62;
for (let i = 0; i++ < 4;) {
let r = Math.random() * (max - min) + min << 0;
str += String.fromCharCode(r += r > 9 ? r < 36 ? 55 : 61 : 48);
}
return str;
}
function generateUniqSerial() {
return `${rnd('a')}-${rnd('n')}-${rnd('a')}-${rnd('n')}`
}
console.log(generateUniqSerial())
console.log(generateUniqSerial())