1

We use this function to generate GUID's and assign to records:

var genguid = function b(a) {
    return a ? (a ^ Math.random() * 16 >> a / 4).toString(16) : ([1e6] + '').replace(/[018]/g, b)
}

The issue is, we're starting to get duplicates, from multiple devices.

The random part could use a salt, possible from the current time or location (since these are used on mobile devices).

Any suggestions on what function / method to use to obtain that?

UPDATE: The code runs on a mobile app which uses JS. I just ran the old code on 1 trillion GUID's and there were no duplicates. So i'm guessing it's something with the JS implementation on the mobile client?

R0b0tn1k
  • 4,256
  • 14
  • 46
  • 64
  • 1
    Why would generating a GUID depend on a passed-in parameter? In the case that `a` isn't passed in, it doesn't look very "globally unique" at all.... – spender Nov 23 '15 at 12:33
  • 2
    Sounds a lot like [this article](https://medium.com/@betable/tifu-by-using-math-random-f1c308c4fd9d). A possible solution is described [here](http://stackoverflow.com/a/2117523/893780) (_"Modern Browsers"_). – robertklep Nov 23 '15 at 12:34
  • Here's the funny part. The code runs on a mobile app which uses JS. I just ran the old code on 1 trillion GUID's and there were no duplicates. So i'm guessing it's something with the JS implementation on the mobile client? – R0b0tn1k Nov 23 '15 at 13:22
  • 2
    @R0b0tn1k the article I referred to states that especially V8 has a poor `Math.random()` implementation, so Chrome and related browsers are more prone to generate collisions using it. – robertklep Nov 23 '15 at 13:49

1 Answers1

0

This function can be used to ensure that there are no duplicates.

https://github.com/dustinpoissant/GUIDJS

var myGuid = GUID();
Dustin Poissant
  • 3,201
  • 1
  • 20
  • 32