I have a unique field in a SQL table that currently has 200K rows
I use randomString(6, '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ')
to insert data on that field, I have too many unique conflict error when I want to insert new rows
In my log , I see that randomString
generated HEGDDX
string today but it generated in 3 months ago too and I have an error on insert
'0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ' has 36 character , I generate 6 length random string, So there is 36^6=2176782336 = 2.17E9 possible cases, So 200K rows in 2 billion has 0.00009 possibility for duplication
Is 0.00009
big enough for too may errors? is Math.random
bad random generator? what is alternative for me?
const randomString = function(length, chars) {
let str = '';
const charsLen = chars.length;
for (let i = 0; i < length; i++) {
str += chars.charAt(Math.floor(Math.random() * charsLen));
}
return str;
}
Aucun commentaire:
Enregistrer un commentaire