in reply to Re^5: Generate a unique ID
in thread Generate a unique ID
The odds of even one name-collision are extremely small;
Do you consider a 1 in 30 chance as "extremely small"?
>perl -E"$h{rand()}++ for 1..1e6; printf qq[prob: %.3f%%\n], (keys(%h) +/1e4)" prob: 3.277% >perl -E"$h{rand()}++ for 1..1e6; printf qq[prob: %.3f%%\n], (keys(%h) +/1e4)" prob: 3.277% >perl -E"$h{rand()}++ for 1..1e6; printf qq[prob: %.3f%%\n], (keys(%h) +/1e4)" prob: 3.277% >perl -E"$h{rand()}++ for 1..1e6; printf qq[prob: %.3f%%\n], (keys(%h) +/1e4)" prob: 3.277%
What I've implemented is this (the code is to be part of an XS module):
void makeDir( void ) { in t i = 10; do { sprintf( dir, "c:/tmp/MYAPP04x%04X/", GetCurrentProcessId(), GetTickCount64() & 0xffff ); --i || expire( -99999 ); } while( _mkdir( dir ) == ERROR_FILE_EXISTS ); GetLastError() && expire( - GetLastError() ); return; }
GetTickCount64() returns the uptime in milliseconds. By truncating it to 16-bits it proves to be a better rand() than MS' CRT rand() :).
The error codes are provisional!
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^7: Generate a unique ID
by locked_user sundialsvc4 (Abbot) on Nov 17, 2010 at 17:22 UTC | |
by BrowserUk (Patriarch) on Nov 17, 2010 at 17:58 UTC | |
|
Re^7: Generate a unique ID
by Anonymous Monk on Nov 17, 2010 at 15:38 UTC | |
by BrowserUk (Patriarch) on Nov 17, 2010 at 15:42 UTC |