views:

103

answers:

2

I want to generate a 16 character alphanumeric Session ID string. What is the best way to do this so that it is guaranteed that the string generated will be unique every time?

Note: I will be using C++ to generate the session IDs.

+3  A: 

Typically you would create a GUID using whatever language/OS services provide such a service. Commonly available GUIDs are 16 bytes long, which in hex representation would be 32 characters. You can base64 encode that and get it slightly smaller (22 characters or so). Do you really need exactly 16 characters?

Greg Hewgill
How do I create a GUID in C++ for UNIX? Are there APIs available in C++?
On Linux and some other platforms, you can use `libuuid`: http://linux.die.net/man/3/libuuid Or, you can use the `uuidgen` shell command.
Greg Hewgill
A: 

Use a GUID. How you generate one will depend on the platform you're running on.

John Saunders

related questions