views:

214

answers:

1

I need to generate unique ids in the browser. Currently, I'm using this:

Math.floor(Math.random() * 10000000000000001)

I'd like to use the current UNIX time ((new Date).getTime()), but I'm worried that if two clients generate ids at the exact same time, they wouldn't be unique.

Can I use the current UNIX time (I'd like to because that way ids would store more information)? If not, what's the best way to do this (maybe UNIX time + 2 random digits?)

+6  A: 

Hi Horace, you can create a GUID using the following links:

http://softwareas.com/guid0-a-javascript-guid-generator

http://www.ajaxwith.com/GUID-for-JavaScript.html

http://stackoverflow.com/questions/105034/how-to-create-a-guid-uuid-in-javascript

This will maximise your chance of "uniqueness."

Alternatively, if it is a secure page, you can concatenate the date/time with the username to prevent multiple simultaneous generated values.

Russell
I would go with the last suggestion, Concatenate with DATE/TIME with username.
kayteen
+1 for "concatenate the date/time with the username to prevent multiple simultaneous generated values.".
Imagist