in reply to Random, Unique, but Simple session ID

Use some combination of the current date/time, a random number, and possibly a checksum to improve validity. Eg:
my $uniqueId = time()*10000 + int rand (10000); $uniqueId = $uniqueId*100 + ( ( ( $uniqueId div 10000000) % 10 ) * ( ( $uniqueId div 100 ) % 10 ) );
The latter uses the 8th and 3rd digit from the right as checksums in this case.
Dr. Michael K. Neylon - mneylon-pm@masemware.com || "You've left the lens cap of your mind on again, Pinky" - The Brain

Replies are listed 'Best First'.
Re: Re: Random, Unique, but Simple session ID
by Fastolfe (Vicar) on Apr 13, 2001 at 23:24 UTC
    Not to nit-pick, but a 'random number' is not guaranteed to be different the next time it's requested. There is a possibility two people will hit it within a second of each other (thus getting the same time) and get the same random number, thus giving them the same unique ID. Granted, the probability is low, but if you get a lot of ID requests, this will be a very real concern. I usually combine the process ID of the process making the request in with that, since it's highly unlikely you will iterate through the system's available process pool within 1 second of time, and combined with a random number like you're doing, makes it for all practical purposes completely unique.