Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Random File Selection

Status
Not open for further replies.

Vec

IS-IT--Management
Jan 29, 2002
418
US
I am opening an swf file using a browser object like this:

WebBrowser1.Navigate "file:\\C:\001.swf"

What I need to do is randomize the swf selection between a set number of files. The files are named 001.swf, 002.swf and so on up to 050.swf so I need the code to run like this:
Pseudo Code:
Choose a random number between 001 and 050
Replace the word RANDOM with the selected number in:
WebBrowser1.Navigate "file:\\C:\RANDOM.swf"
Then run the WebBrowser1.Navigate Event "Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning."
-- Rich Cook.

teklogo.gif

 

Look up the rnd function in help and the Randomize key word. You probably will also need the format function to get the correct format for the file name.

Good Luck

 
for a quick and easy solution you could always do somethng like....

Code:
    Dim number As Double
    Dim filler As String, filename As String
    
    filler = "0"
    
    number = Rnd()
    Do While ((number * 100) > 50)
        number = Rnd()
    Loop

    number = number * 100

    filename = filler & CInt(number) & ".swf"
    If number < 10 Then filename = filler & filename
    WebBrowser1.Navigate &quot;file:\\C:\&quot; & filename
[\code]

u could prolly cut the code a little bit but this is just a fast solution 1001100 1110101 1101011  1100101
 
looks like vb5prgrmr beat me to it.... oh well

Happy Trails 1001100 1110101 1101011 1100101
 

OK EnCocytus, if you are going to try to show some code let me help you out with the rnd function right from the example in the help file...
[tt]
Rnd Function Example
This example uses the Rnd function to generate a random integer value from 1 to 6.

Dim MyValue
MyValue = Int((6 * Rnd) + 1) ' Generate random value between 1 and 6.
[/tt]
so for Vec's solution...
[tt]
Dim MyValue
MyValue = Int((50 * Rnd) + 1) ' Generate random value
MyValue = Format(MyValue,&quot;000&quot;)
MyValue = &quot;file:\\C:\&quot; & MyValue & &quot;.swf&quot;
[/tt]
This also points out the fact at how versitle the variant is.

Good Luck


 
No need to get touchy vb5prgrmr, i'll admit I'm not a vb savant, being that I don't have any help files to reference (besides online that is). Just trying to do my share in helping out. I never said that my code was the definitive way of doing it.... just a quick solution.

As far as using variants, I realize the versatility, but prefer not using them. Just as a personal style.

However, thank you for your advice. 1001100 1110101 1101011 1100101
 

EnCocytus,

Not touchy, it just seemed to me that your rnd function you gave was ... a little inefficient, and could cause a version of delayed contention. I also agree with you in the fact that I do not use variants either except when needing to coheres a ULargeInt or floating point into a decimal.

Good Luck


 
My english is not very good but I would like to cooperate with you guys. this code I provide is a more generic way to get any random selected file. Of course, some changes may be necessary but it work fine!

To test: create a new project, insert a push button (Command1) as past the following code:


Option Explicit
Option Base 1

Private Sub Command1_Click()

Dim m_strFilename As String

If Not GetRandomFile(m_strFilename) Then
MsgBox &quot;Sorry, not even one file were found!&quot;
Else
MsgBox &quot;Random selected file: &quot; & m_strFilename
End If

'Now, use the returned filename as you want...

End Sub

Private Function GetRandomFile(ByRef parm_strFilename As String) As Boolean

Dim m_strSWFFilesArray() As String
Dim m_strThisFile As String
Dim m_intIndex As String
Dim m_strSelectedFilename As String
Dim m_strMyPath As String

m_strMyPath = &quot;c:\temp\random\&quot; 'IMPORTANT: enter the path for your files HERE...
'You may want to use an argument in the function to receive
'the appropriate path... Fill free to change the code!

'load an array with available SWF files in the specified path...
m_strThisFile = Dir(m_strMyPath & &quot;*.SWF&quot;, vbArchive) 'look for files in the path...
If m_strThisFile <> &quot;&quot; Then
'found a file! It must be stored in the array...
m_intIndex = 1
ReDim m_strSWFFilesArray(m_intIndex) 'reallocate storage space for the filename in the array...
m_strSWFFilesArray(m_intIndex) = m_strMyPath & UCase(m_strThisFile)
m_strThisFile = UCase(Dir) 'look for the next filename...
End If

Do 'inictiates a loop to recognize and get all desired files in the path...
If m_strThisFile <> &quot;&quot; Then
m_intIndex = m_intIndex + 1
ReDim Preserve m_strSWFFilesArray(m_intIndex)
m_strSWFFilesArray(m_intIndex) = m_strMyPath & UCase(m_strThisFile)
End If
m_strThisFile = UCase(Dir)
Loop Until m_strThisFile = &quot;&quot;

'now you´ll have (maybe...) an array filled with the
'names of all SWF files found in the specified path...
'...let´s see how many files we have...
Select Case UBound(m_strSWFFilesArray)
Case 0
'nothing to do: no files were found. Let´s get out of here!
parm_strFilename = &quot;&quot; 'no filename will be returned
GetRandomFile = False 'must return false
Case 1
'Get this file: it´s all you have!!!
parm_strFilename = m_strSWFFilesArray(1)
GetRandomFile = True
Case Else
'There´s more than one: let´s random select one of then...
'Next, we will generate a random value that shoud be used
'as a pointer to an element in the array...
Randomize Timer 'Initializes the random-number generator...
'Question: Why? Answer: Why not?!?!?!
m_intIndex = Int(UBound(m_strSWFFilesArray) * Rnd + 1) 'get a random number between 1 and
'the number of elements in the array...
parm_strFilename = m_strSWFFilesArray(m_intIndex) 'got it...?!?!?!?
GetRandomFile = True 'THAT´s IT!!!!
End Select

End Function


-----------------------------
Hope it fit your needs!
Good Luck.
 
Sorry people, the code I posted right before this one has two erros in the GetRandomFile function. Here is the reviewed code for the GetRandomFile() function:

--------------------------------
Private Function GetRandomFile(ByRef parm_strFilename As String) As Boolean

Dim m_strSWFFilesArray() As String
Dim m_strThisFile As String
Dim m_intIndex As String
Dim m_strSelectedFilename As String
Dim m_strMyPath As String

m_intIndex = 0
m_strMyPath = &quot;c:\temp\random\&quot; 'IMPORTANT: enter the path for your files HERE...
'You may want to use an argument in the function to receive
'the appropriate path... Fill free to change the code!

'load an array with available SWF files in the specified path...
m_strThisFile = Dir(m_strMyPath & &quot;*.SWF&quot;, vbArchive) 'look for files in the path...
If m_strThisFile <> &quot;&quot; Then
'found a file! It must be stored in the array...
m_intIndex = 1
ReDim m_strSWFFilesArray(m_intIndex) 'reallocate storage space for the filename in the array...
m_strSWFFilesArray(m_intIndex) = m_strMyPath & UCase(m_strThisFile)
m_strThisFile = UCase(Dir) 'look for the next filename...
End If

Do 'inictiates a loop to recognize and get all desired files in the path...
If m_strThisFile <> &quot;&quot; Then
m_intIndex = m_intIndex + 1
ReDim Preserve m_strSWFFilesArray(m_intIndex)
m_strSWFFilesArray(m_intIndex) = m_strMyPath & UCase(m_strThisFile)
Else
Exit Do
End If
m_strThisFile = UCase(Dir)
Loop Until m_strThisFile = &quot;&quot;

'now you´ll have (maybe...) an array filled with the
'names of all SWF files found in the specified path...
'...let´s see how many files we have...
Select Case m_intIndex
Case 0
'nothing to do: no files were found. Let´s get out of here!
parm_strFilename = &quot;&quot; 'no filename will be returned
GetRandomFile = False 'must return false
Case 1
'Get this file: it´s all you have!!!
parm_strFilename = m_strSWFFilesArray(1)
GetRandomFile = True
Case Else
'There´s more than one: let´s random select one of then...
'Next, we will generate a random value that shoud be used
'as a pointer to an element in the array...
Randomize Timer 'Initializes the random-number generator...
'Question: Why? Answer: Why not?!?!?!
m_intIndex = Int(UBound(m_strSWFFilesArray) * Rnd + 1) 'get a random number between 1 and
'the number of elements in the array...
parm_strFilename = m_strSWFFilesArray(m_intIndex) 'got it...?!?!?!?
GetRandomFile = True 'THAT´s IT!!!!
End Select

End Function

Bye!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top