473,799 Members | 2,985 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Select random files out of a directory

for example, there are 10K files in the directory,

given that i don't know if files name, is it possible to fetch a file
randomly?

thanks.

Jan 5 '07
10 10655
-----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-----
Jan 5 '07 #11

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
3418
by: kingofkolt | last post by:
I have a directory of images, called "random". In it are the following files: 1.gif 2.gif 3.gif 4.gif I use this script to choose a random image and display it:
6
20707
by: Acacia | last post by:
How would you generate a random number in C++?
4
3802
by: SoulSniper | last post by:
Hi, I have been stuck on this for a few days now and have given up trawling through pages and pages of google results.. I'm just putting the finishing touches to a small game I've written. The game uses the FMOD sound library to play an MP3 as background music. I got slightly bored of listening to the same song over and over and realised it would be much nicer if the game could pick an MP3 at random from a folder...
4
2058
by: brett | last post by:
On my website I have a directory filled with possible files that a user can select to download. Rather than going to the directory itself to download, I'd like the user to be able to select a single file from a select box embedded on an html page. I'm looking for a way that I can use javascript to do this for me. I know that perl could get these filenames for me easily, but is javascript able to put these into a select box once I have Perl...
5
2953
by: Raterus | last post by:
I'm just throwing this error out for my sanity, I've seen posts about this, but never solutions. I'm using VS.NET 2003, Framework 1.1, and I'm getting a random error about every 1 out of 10 times I try to start to run/debug a project (through Visual Studio) Server Error in '/Intranet' Application. -------------------------------------------------------------------------------- Compilation Error Description: An error occurred during...
1
2667
by: Sahus Pilwal | last post by:
Hi, I hope someone can help me with this. I'm new to .NET and in fact server side programming and have a small query I'm sure... I'm using the System.IO Namespace with a For - each and If then statement to randomly generate images to display in a <asp:image/image control. The random images are selected from a fixed folder on the webserver. All works fine but only if the images in the folder are of image format say jpg or gif. However...
39
2475
by: Alan Isaac | last post by:
This may seem very strange, but it is true. If I delete a .pyc file, my program executes with a different state! In a single directory I have module1 and module2. module1 imports random and MyClass from module2. module2 does not import random. module1 sets a seed like this::
2
1120
by: arvindfs | last post by:
Hi, I have about 10 files in a directory and need to select them in random order every time I run the program. I guess that this can be done using commands "Dir" and "Rnd". Can someone help me? Thanks in advance.
0
9540
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10250
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10222
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9068
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7564
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6805
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5585
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3757
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2938
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.