randomising

If i have a string array - how can i randomise the positions of the entries in the array.

Thanks
wormboy__6Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

adityauCommented:
Use Rnd function.
0
tylerdCommented:
dim i as integer
dim rand as integer
dim newstr as string

for i = 1 to len(str)
   rand = rnd * len(str)    
   newstr = newstr & mid(str, rand, 1)
next i

str = newstr



where str is the string you want to scramble
0
wormboy__6Author Commented:
I am not randomising letters in a string - i am randomising strings in a string array...could you do this?
0
Big Business Goals? Which KPIs Will Help You

The most successful MSPs rely on metrics – known as key performance indicators (KPIs) – for making informed decisions that help their businesses thrive, rather than just survive. This eBook provides an overview of the most important KPIs used by top MSPs.

raymerCommented:
Why don't you post a code snippet of what your string array looks like (fixed-length, dynamic, dimensions, etc...) and explain a little more clearly what it is you want to do so we don't waste time?

Ray Mercer
MS-MVP Visual Basic
www.shrinkwrapvb.com
0
wormboy__6Author Commented:
ok
   
    Dim Servers() As String
    ReDim Servers(5)
    Servers(1) = "http://test.com"
    Servers(2) = "http://hi.net"
    Servers(3) = "http://black.org"
    Servers(4) = "http://house.com"
    Servers(5) = "http://eat.net"

Could you write a piece of code to randomise the position of these values in the array Servers.

cheers
0
raymerCommented:
Hi,
The code follows fo a demo - stick a button on a form and paste it in.

Note that this will ignore the Servers(0) member in your sample above.  If you want to use zero-based arrays just remove the "+ 1" from the end of the line where "lowPos" is initialized.

Enjoy!
Ray Mercer
MS-MVP Visual Basic
www.shrinkwrapvb.com

--


Option Explicit
Private stringarray() As String

Private Sub Form_Load()
ReDim stringarray(5)
stringarray(1) = "one"
stringarray(2) = "two"
stringarray(3) = "three"
stringarray(4) = "four"
stringarray(5) = "five"
End Sub

Sub RandomizeStringArray(ByRef strArray() As String)

Dim numEntries As Long
Dim i As Long
Dim rndPos As Long
Dim lowPos As Long
Dim hiPos As Long
Dim tmpString As String

On Error Resume Next 'errors on next line will fall thru to "Else" block
If IsNumeric(UBound(strArray)) Then 'array is properly initialized
    On Error GoTo 0 'turn off error trap
    Randomize 'init VB internal random number generator
    hiPos = UBound(strArray)
    Debug.Print hiPos
    lowPos = LBound(strArray) + 1 'only add "+ 1" if you don't want to use the strArray(0) member
    Debug.Print lowPos
    For i = hiPos To lowPos Step -1
         rndPos = Int((i - lowPos + 1) * Rnd + lowPos)
         tmpString = strArray(rndPos)
         strArray(rndPos) = strArray(i)
         strArray(i) = tmpString
         
      Next
     
   
Else
    'errors will come here
    MsgBox "the string array has not been initialized!, vbInformation, app.title"

End If


End Sub

Private Sub Command1_Click()

Call RandomizeStringArray(stringarray)
Me.Print stringarray(1)
Me.Print stringarray(2)
Me.Print stringarray(3)
Me.Print stringarray(4)
Me.Print stringarray(5)

End Sub
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
raymerCommented:
Dang! That's what I get for trying to be fancy with my error handling...
My original answer works fine as long as the string array is initialized, but my funky error trap is broken.  I moved the Sub to a function that returns false if the string array does not get shuffled properly - this version is more professional.

Hope this helps,
Ray Mercer
MS-MVP Visual Basic
www.shrinkwrapvb.com

--

Option Explicit
Private stringarray() As String

Private Sub Form_Load()
ReDim stringarray(5)
stringarray(1) = "one"
stringarray(2) = "two"
stringarray(3) = "three"
stringarray(4) = "four"
stringarray(5) = "five"
End Sub

Function RandomizeStringArray(ByRef strArray() As String) As Boolean

Dim numEntries As Long
Dim i As Long
Dim rndPos As Long
Dim lowPos As Long
Dim hiPos As Long
Dim tmpString As String

On Error GoTo NotInitialized 'errors on next line will fall thru to "Else" block
Randomize 'init VB internal random number generator
hiPos = UBound(strArray)
lowPos = LBound(strArray) + 1 'only add "+ 1" if you don't want to use the strArray(0) member
For i = hiPos To lowPos Step -1
     rndPos = Int((i - lowPos + 1) * Rnd + lowPos)
     tmpString = strArray(rndPos)
     strArray(rndPos) = strArray(i)
     strArray(i) = tmpString
Next
RandomizeStringArray = True
Exit Function
NotInitialized:
'errors will come here and function will return false

End Function

Private Sub Command1_Click()

If RandomizeStringArray(stringarray) Then
    Me.Print stringarray(1)
    Me.Print stringarray(2)
    Me.Print stringarray(3)
    Me.Print stringarray(4)
    Me.Print stringarray(5)
Else
    MsgBox "the string array has not been initialized!", vbInformation, App.Title
End If

End Sub
0
wormboy__6Author Commented:
thanks
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic Classic

From novice to tech pro — start learning today.