Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Random file from a directory

Reply
Thread Tools

Random file from a directory

 
 
SoulSniper
Guest
Posts: n/a
 
      09-01-2004
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...

Problem is, I can't figure out how to get the game to pick a random
file from a folder. I've been searching google for days and found
nothing of any use...

Any ideas, anyone?
 
Reply With Quote
 
 
 
 
Jack Klein
Guest
Posts: n/a
 
      09-01-2004
On 31 Aug 2004 18:19:09 -0700, http://www.velocityreviews.com/forums/(E-Mail Removed) (SoulSniper) wrote
in comp.lang.c++:

> 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...
>
> Problem is, I can't figure out how to get the game to pick a random
> file from a folder. I've been searching google for days and found
> nothing of any use...
>
> Any ideas, anyone?


By the use of the lame Microsoft-ism "folder" for what everyone else
in the world called a directory, I'd suggest you ask in
news:comp.os.ms-windows.win32.programmer or one of Microsoft's
supports groups in the news:microsoft.public.vc.* family.

It's off-topic here because standard C++ has no support whatsoever for
"folders", or even real directories.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
 
Reply With Quote
 
 
 
 
Vladimir Ciobanu
Guest
Posts: n/a
 
      09-01-2004
"SoulSniper" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) om...
> 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...
>
> Problem is, I can't figure out how to get the game to pick a random
> file from a folder. I've been searching google for days and found
> nothing of any use...


The easiest way would be to store every filename in a std::vector<
std::string > and get a random number between 0 and vector::size().
The random file would be vector[number].

Vladimir Ciobanu


 
Reply With Quote
 
Karthik Kumar
Guest
Posts: n/a
 
      09-01-2004
SoulSniper wrote:

> 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...
>
> Problem is, I can't figure out how to get the game to pick a random
> file from a folder. I've been searching google for days and found
> nothing of any use...
>
> Any ideas, anyone?


Assuming N to be the number of files in the given directory, the
given problem reduces to picking a random number generator in the range
1 .. N . Look for a good random number generator, ( I am sure boost
library should have one) and that should get your job done.

--
Karthik.
 
Reply With Quote
 
Howard
Guest
Posts: n/a
 
      09-01-2004

"Jack Klein" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> On 31 Aug 2004 18:19:09 -0700, (E-Mail Removed) (SoulSniper) wrote
>
> By the use of the lame Microsoft-ism "folder" for what everyone else
> in the world called a directory, I'd suggest you ask in
> news:comp.os.ms-windows.win32.programmer or one of Microsoft's
> supports groups in the news:microsoft.public.vc.* family.
>


FYI, the (fairly) new Mac operating system, OS X, refers to those as
"folders" as well, and even displays icons on the desktop as your typical
office folders. So... not "everyone else in the world" calls them
directories.

As for how to accomplish the task, you're right that methods for listing
directory (or folder) contents is off-topic here.

However, a hint might be given, that you could read the directory list into
a container, select a random index into that container, and then attempt to
use the file referenced by that entry. (But don't forget the obvious...to
include some kind of protection against the file you want no longer being
there!)

-Howard



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Math.random() and Math.round(Math.random()) and Math.floor(Math.random()*2) VK Javascript 15 05-02-2010 03:43 PM
random.random(), random not defined!? globalrev Python 4 04-20-2008 08:12 AM
System.IO.Directory.GetDirectories() and System.IO.Directory.GetFiles() are not returning the specified directory Nathan Sokalski ASP .Net 2 09-06-2007 03:58 PM
is Random Access File really "random access"? Kevin Java 19 02-13-2006 09:31 PM
Random NOt random? Darren Clark ASP .Net 3 06-24-2004 05:23 PM



Advertisments