Forum Discussion

yash_verma's avatar
yash_verma
New Contributor
5 years ago

How to generate unique guids in snaplogic

Hi
I am using math.random() function of mapper component to generate unique guids. I am worried that after repetitive use of this function it will generate duplicate results. Is there any way i can be sure that it doesnt generate same guids. Is there any other efficient way of doing this. Please let me know.

Thanks

Yash

6 Replies

  • yash_verma's avatar
    yash_verma
    New Contributor

    sorry I am using math. Randomuuid () only. So this function you are saying will never return same guids.? Pls let me know as if it return duplicates than my solution will be ruined.

    • ptaylor's avatar
      ptaylor
      Employee

      I see. Well, a random UUID is a 128-bit random value, so repeated values are VERY unlikely. But if you wanted to guarantee uniqueness, you could concatenate a timestamp:

      Math.randomUUID() + Date.now().getTime()

  • yash_verma's avatar
    yash_verma
    New Contributor

    Yes i can do that but it wont be a 128 bit guid if i add date. Anyother option u would like to propose.

    • ptaylor's avatar
      ptaylor
      Employee

      No. I’d just use a randomUUID. You’re extremely unlikely to get repeats. We use them all the time when we need unique values and I’m not aware of any issues related to repeats.

      • ptaylor's avatar
        ptaylor
        Employee

        Actually, how about this?

        (Date.now().getTime() + Math.randomUUID()).substr(0,36)

        That will give you a value like 16008933027615cf16319-1480-4260-9c84. The time component changes every millisecond, plus a random component, and it’s the length you want.