473,770 Members | 2,147 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Random file from a directory

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?
Jul 22 '05 #1
4 3801
On 31 Aug 2004 18:19:09 -0700, so********@gmai l.com (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.p rogrammer 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.l earn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
Jul 22 '05 #2
"SoulSniper " <so********@gma il.com> wrote in message
news:17******** *************** ***@posting.goo gle.com...
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
Jul 22 '05 #3
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.
Jul 22 '05 #4

"Jack Klein" <ja*******@spam cop.net> wrote in message
news:96******** *************** *********@4ax.c om...
On 31 Aug 2004 18:19:09 -0700, so********@gmai l.com (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.p rogrammer 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

Jul 22 '05 #5

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

Similar topics

4
3417
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:
12
9711
by: bhennon | last post by:
Hey all, I have a small php script that calls a random image at the following page. http://www.2006ymcanationals.com/random.php IT WORKS IF I go directly to the above link. I am trying to call that in another page so that i get a random image the page is http://2006ymcanationals.com/index.php using <img
3
6376
by: Rob B | last post by:
Hello, I am just starting to learn Python and was writing a simple script on my machine (Mac OS X 10.3.4), but I can't seem to import the random module: #!/usr/bin/env python import random
6
20706
by: Acacia | last post by:
How would you generate a random number in C++?
4
7236
by: Baz | last post by:
I want to randomly select some files from a source directory & copy them to a target folder for testing. How can I select files from the source directory randomly? I have thought about getting a count of the total files in the folder & then using a random number generator to select files based on count. Is there another more efficient way of doing this, something that doesn't involve generating a random number? thanks!
14
9406
by: DataSmash | last post by:
Hi, When I import the random module at the python interpreter, it works fine: >>> import random >>> x = random.randint(1,55) >>> print x 14 >>> BUT, when I put the same code in a python script:
10
10653
by: howa | last post by:
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.
39
2471
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::
3
2425
by: Andrew F | last post by:
I'm a linux user and I just upgraded from 2.1 to 2.5 and changed the location of a number of libraries and tools. So far I've tracked most errors, but this one has me scratching my head : $ which python /usr/local/bin/python $ echo $PYTHONPATH /usr/local/bin/python
0
9619
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9454
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
10260
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10038
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
8933
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
7460
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
5354
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4007
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2850
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.