Random file from a directory

Discussion in 'C++' started by SoulSniper, Sep 1, 2004.

  1. SoulSniper

    SoulSniper Guest

    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?
     
    SoulSniper, Sep 1, 2004
    #1
    1. Advertising

  2. SoulSniper

    Jack Klein Guest

    On 31 Aug 2004 18:19:09 -0700, (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/~ajo/docs/FAQ-acllc.html
     
    Jack Klein, Sep 1, 2004
    #2
    1. Advertising

  3. "SoulSniper" <> wrote in message
    news:...
    > 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
     
    Vladimir Ciobanu, Sep 1, 2004
    #3
  4. 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.
     
    Karthik Kumar, Sep 1, 2004
    #4
  5. SoulSniper

    Howard Guest

    "Jack Klein" <> wrote in message
    news:...
    > On 31 Aug 2004 18:19:09 -0700, (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
     
    Howard, Sep 1, 2004
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Darren Clark

    Random NOt random?

    Darren Clark, Jun 24, 2004, in forum: ASP .Net
    Replies:
    3
    Views:
    469
    mikeb
    Jun 24, 2004
  2. Maziar Aflatoun

    Random not really random...

    Maziar Aflatoun, Aug 4, 2004, in forum: ASP .Net
    Replies:
    4
    Views:
    26,746
    Maziar Aflatoun
    Aug 5, 2004
  3. Kevin
    Replies:
    19
    Views:
    1,146
    Tris Orendorff
    Feb 13, 2006
  4. globalrev
    Replies:
    4
    Views:
    782
    Gabriel Genellina
    Apr 20, 2008
  5. VK
    Replies:
    15
    Views:
    1,210
    Dr J R Stockton
    May 2, 2010
Loading...

Share This Page