wormboy__6
asked on
randomising
If i have a string array - how can i randomise the positions of the entries in the array.
Thanks
Thanks
Use Rnd function.
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
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
ASKER
I am not randomising letters in a string - i am randomising strings in a string array...could you do this?
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
Ray Mercer
MS-MVP Visual Basic
www.shrinkwrapvb.com
ASKER
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
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
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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(strin garray) 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
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
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(strin
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
ASKER
thanks