Solved

# randomising

Posted on 2000-01-03
Medium Priority
161 Views
If i have a string array - how can i randomise the positions of the entries in the array.

Thanks
0
Question by:wormboy__6

LVL 2

Expert Comment

ID: 2321945
Use Rnd function.
0

LVL 2

Expert Comment

ID: 2322977
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

Author Comment

ID: 2328043
I am not randomising letters in a string - i am randomising strings in a string array...could you do this?
0

Expert Comment

ID: 2328186
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

Author Comment

ID: 2330118
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

Accepted Solution

raymer earned 120 total points
ID: 2330794
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

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

Expert Comment

ID: 2330813
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

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

Author Comment

ID: 2376127
thanks
0

## Featured Post

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.