-3

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.

adnan kamili
  • 8,967
  • 7
  • 65
  • 125
  • 1
    You want a universally-unique string that's largely alphanumeric? That sounds an awful lot like [a UUID](https://en.wikipedia.org/wiki/Universally_unique_identifier), which your database probably supports already. – ssube Sep 15 '16 at 18:14
  • @ssube uuid is 128 bit – adnan kamili Sep 15 '16 at 18:16
  • 1
    Yes, UUIDs are 128 bits. That's significantly smaller than your 25-character identifier, while still being universally unique **and** optimized in most databases for fast lookups. – ssube Sep 15 '16 at 18:18
  • @ssube I haven't mentioned that I am going to use it for lookups and want to improve the lookup performance – adnan kamili Sep 15 '16 at 18:26
  • Performance has little to do with it. The database is the only actor able to guarantee that a value is unique, so it should be the one producing those values. Most databases have fantastic UUID support, which happens to include performance optimizations. – ssube Sep 15 '16 at 18:35

1 Answers1

3

Is this enough to ensure that MySQL unique constraint won't be violated by the random strings.

3625 has 129 bit. If we apply the birthday problem, then you're likely getting a collision around 264 strings. You'll probably generate much less than that. This is only true provided that you use a good randomness source.

Math.random() is not a good randomness source.

Artjom B.
  • 61,146
  • 24
  • 125
  • 222
  • Doesn't appending timestamp and math.random() to generate seed guarantee a unique number atleast every nanosecond which should be enough if I am not using more than 4 node servers in a cluster – adnan kamili Sep 15 '16 at 18:57
  • It's not guaranteed, but it's pretty unlikely. Also, you can even solve this without any randomness at all. Just give each node in the cluster a different identifier (a single character is enough). It's unlikely, but if two requests are handled at the same time in a single node server, then you can use a counter. Concatenate the node id, the counter and the timestamp in order to get a short and guaranteed unique ID. – Artjom B. Sep 15 '16 at 19:04