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!
In reply to Re^6: Generate a unique ID
by BrowserUk
in thread Generate a unique ID
by BrowserUk
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |