I have a use case where I need to generate alpha numeric capital case strings of length 25, so the total possible unique combinations are very high:
36 pow (25) = 808281277464764060643139600456536293376
The string is to be stored in MySql
database table with unique
set to true
I am using following code to generate the string:
const Chance = require('chance');
const chance = new Chance(Date.now() + Math.random());
let randomStr = chance.string({length: 25,
pool: 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789'});
console.log(randomStr);
Node.js can run in cluster mode, so value of timestamp can be same for different requests so I also added Math.random()
. Is this enough to ensure that MySql
unique constraint won't be violated by the random strings.