473,396 Members | 1,758 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Randomly Assigning a File

Hello,

Can someone tell me how I can randomly assign a file to a
variable from a directory.

Example:
Dim File as string

I need to search through 'C:/Comics/' and have the
program randomly pick a file from that path and assign it
to the variable 'File'

I will use this info to string together an image source
to randomly chage a picture. I am not interested in
using the ad rotator for this purpose.

Thanks.
Nov 19 '05 #1
3 1473
Hello,

"brian" <br****@lbrspec.com> schrieb:
Can someone tell me how I can randomly assign a file to a
variable from a directory.

Example:
Dim File as string

I need to search through 'C:/Comics/' and have the
program randomly pick a file from that path and assign it
to the variable 'File'

I will use this info to string together an image source
to randomly chage a picture. I am not interested in
using the ad rotator for this purpose.


Untested:

\\\
Imports System.IO
..
..
..
Dim oRand As New Random(Environment.TickCount)
Dim files() As String = Directory.GetFiles("C:\")
Dim i As Integer = oRand.Next(0, files.Length - 1)
MsgBox(files(i))
///

HTH,
Herfried K. Wagner
--
MVP · VB Classic, VB .NET
http://www.mvps.org/dotnet
Nov 19 '05 #2
Thanks for the reply Tom.

Can you please explain something to me. What does
'Dim intNumber As Integer = CInt(rFile.NextDouble() *
(strFiles.Length -1))' Do?

I notice you assign this to an integer and the integer is
returned. How do you then tell the image source what
file name to use since you are not looking for a file
name but instead an integer?
-----Original Message-----
Hi, try this:

Imports System.IO
..
..
..
Public Function GetRandomFile(ByVal strDirectory As String) As String Dim strFiles() As String = Directory.GetFiles (strDirectory) Dim rFile As New Random
Dim intNumber As Integer = CInt(rFile.NextDouble() * (strFiles.Length -1))

Return strFiles(intNumber)

End Function

--
Happy to help,
-- Tom Spink
(th**********@ntlworld.com)

"Go down with your server"

http://dotnetx.betasafe.com >> On The Mend

Please respond to the newsgroup,
so all can benefit
"brian" <br****@lbrspec.com> wrote in message
news:03****************************@phx.gbl...
Hello,

Can someone tell me how I can randomly assign a file to a variable from a directory.

Example:
Dim File as string

I need to search through 'C:/Comics/' and have the
program randomly pick a file from that path and assign it to the variable 'File'

I will use this info to string together an image source
to randomly chage a picture. I am not interested in
using the ad rotator for this purpose.

Thanks.

.

Nov 19 '05 #3
"Tom Spink" <th**********@ntlworld.com> wrote in
news:ew*************@TK2MSFTNGP09.phx.gbl:
Dim intNumber As Integer = CInt(rFile.NextDouble() *
(strFiles.Length -
1))


The line above seems a little inefficient. You get a double from the
random generator and then convert it to an integer. Why not just get the
integer directly using the Next function?

Dim intNumber As Integer = rFile.Next(0,strFiles.Length)

Just curious about the choice you made.

Chris

Nov 19 '05 #4

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

Similar topics

2
by: James M. | last post by:
I have 2x Front-end Web servers (Win2003 Ent IIS6.0), setup with NLB (single Affinity, also using SQLServer session state) that are hosting a few .NET ASP web applications under the Default Website...
1
by: Benny Ng | last post by:
Hi,All, Export Method: ------------------------------------------------------------------------- strFileNameExport = "Results" Response.Clear() Response.Buffer = True...
4
by: James | last post by:
Just learning C#. What's the easiest way to assign numbers 1-10 randomly to an array? Basically I just want to take the numbers 1-10 and arrange them randomly in slots 0-9 of an array. Thanks
5
by: fbwhite | last post by:
I know this issue has been brought up many times, but I have tried many of the solutions to no avail. I wanted to give my specific case to see if someone could be of any help. We are using the...
2
by: TPK | last post by:
I have an HTML document with Javascript where I have a portion of the page contents, a series of questions, being pulled from a XML file. When the page dynamically builds all the questions and...
9
by: Alan Isaac | last post by:
I need access to 2*n random choices for two types subject to a constraint that in the end I have drawn n of each. I first tried:: def random_types(n,typelist=): types = typelist*n...
10
by: rohit | last post by:
hi, i am developing a desktop search.For the index of the files i have developed an algorithm with which i should be able to read and write to a line if i know its line number. i can read a...
4
by: bb nicole | last post by:
Hi.. i would like to create a page that provide users to randomly run phone number. I know it is use rand() to code it, but how if i porvide a box for user to key in how many phone number they want...
7
by: Man4ish | last post by:
I have one pblm for reading a file randomly for searching the value with in given range. e.g. offset allele id 19 G/T 2066803 20 C/T 2066804 ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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...
0
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,...
0
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.