-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
Curtis schrieb:
$dir = './';
$files = array();
$file = '';
if ( is_dir($dir) ) {
if ( $d = opendir($dir) ) {
while ( ($file = readdir($d)) !== false ) {
if ( !is_dir($file) ) $files[] = $file;
}
}
}
// specify an int for the optional 2nd arg if you
// need more than one random file
$randomKeys = array_rand($fil es);
$randomFile = $files[$randomKeys[0]];
Without the memory overhead selecting one random file could in addition
look like this then:
$dir = './';
$file = '';
mt_srand(time() );
$probability=mt _getrandmax()/10000;
if ( is_dir($dir) ) {
if ( $d = opendir($dir) ) {
while ( ($file = readdir($d)) !== false ) {
if ( !is_dir($file) ) {
if (mt_rand()<$pro bability) break;
}
}
}
}
I know it's far from perfect and it's not equally distributed but might
save some execution time if you accidently select the second file ;) as
well as you don't have the memory overhead and it takes at most as long
as the original script. Important here is the probability variable,
which should be adjusted with an estimated file count of the desired folder.
Stefan
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.2.1 (MingW32)
iD8DBQFFnnuRyeC Lzp/JKjARAgImAJ0WdU 9IjvrF9+vgYqRGQ k3ewvjW/ACfcrAx
i7zjE6lnU58u6Ss ykMualQs=
=4QPJ
-----END PGP SIGNATURE-----