Hi ,
How can i shuffle string of array
alphabets = {"A", "B", "C", "D", "E", "F", "G", "H", "I", "J"};
TO SOME RANDOM STATE LIKE
alphabets = {"C", "A", "I", "D", "G", "J", "H", "H", "B", "F"};
' Knuth's Algorithm P
Dim n As Int = alphabets.Length
Dim r As Int
Dim t As String
For i = 0 To n - 1
r = Rnd(i, n)
t = alphabets(i)
alphabets(i) = alphabets(r)
alphabets(r) = t
Next
' Knuth's Algorithm P
Dim n As Int = alphabets.Length
Dim r As Int
Dim t As String
For i = 0 To n - 1
r = Rnd(i, n)
t = alphabets(i)
alphabets(i) = alphabets(r)
alphabets(r) = t
Next
Thanks for hint MLdev ... i converted it to sub , may be helpful for some Newbie
B4X:
Sub ShuffleArray(arr() As String)
Dim n As Int = arr.Length-1
Dim r As Int
Dim t As String
For i = 0 To n - 1
r = Rnd(i, n)
t = arr(i)
arr(i) = arr(r)
arr(r) = t
Next
Return arr
End Sub
How to use
B4X:
ShuffleArray(YourArr)
B4A forum is wonderful as long as people like you live there...
it worked like a charm...thanks again to you and Knuth's Algorithm P
regards
sanjib
This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
By continuing to use this site, you are consenting to our use of cookies.