pgilfeather
asked on
How do you randomly shuffle an array
Suppose I was working with the following array, how would I randomly rearrange the order?
I am woking with VB.NET code.
Dim MyArray(4) as Integer
MyArray(0) = 6546
MyArray(1) = 6547
MyArray(2) = 6548
MyArray(3) = 6549
MyArray(4) = 6550
Cheers
I am woking with VB.NET code.
Dim MyArray(4) as Integer
MyArray(0) = 6546
MyArray(1) = 6547
MyArray(2) = 6548
MyArray(3) = 6549
MyArray(4) = 6550
Cheers
Duplicated question?
Dim arraySize, j as Integer
Dim temp as Integer ' Must be the same type as array elements
arraySize = UBound(MyArray) - LBound(MyArray) + 1
For i = LBound(MyArray) To UBound(MyArray)
j = Int(arraySize * Rnd)
temp = MyArray(i)
MyArray(i) = MyArray(j)
MyArray(j) = temp
Next i
Dim arraySize, j as Integer
Dim temp as Integer ' Must be the same type as array elements
arraySize = UBound(MyArray) - LBound(MyArray) + 1
For i = LBound(MyArray) To UBound(MyArray)
j = Int(arraySize * Rnd)
temp = MyArray(i)
MyArray(i) = MyArray(j)
MyArray(j) = temp
Next i
Oops! Detected some bug:
j = Int((arraySize-1) * Rnd)+LBound(MyArray)
j = Int((arraySize-1) * Rnd)+LBound(MyArray)
ASKER
Thanks for that guys!
jaime olivares,
I have this so far that when you click a button a listbox shows the numbers in the array. How do I set up your code to work with my array?
Whenever I get code from this site it never seems to be runnable code and being a beginner like me this gets frustrating. You can probaly tell from my code that I've just been introduced to the concept of arrays.
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim MyArray(4) As Integer
MyArray(0) = 6546
MyArray(1) = 6547
MyArray(2) = 6548
MyArray(3) = 6549
MyArray(4) = 6550
lstNumbers.DataSource = MyArray
lstNumbers.DataBind()
End Sub
jaime olivares,
I have this so far that when you click a button a listbox shows the numbers in the array. How do I set up your code to work with my array?
Whenever I get code from this site it never seems to be runnable code and being a beginner like me this gets frustrating. You can probaly tell from my code that I've just been introduced to the concept of arrays.
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim MyArray(4) As Integer
MyArray(0) = 6546
MyArray(1) = 6547
MyArray(2) = 6548
MyArray(3) = 6549
MyArray(4) = 6550
lstNumbers.DataSource = MyArray
lstNumbers.DataBind()
End Sub
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks that worked!
You forgot to dim 'arraySize' and 'i' but nevertheless you've earned yourself 500 points.
I don't fully understand how this works but I'm glad it does.
Cheers
You forgot to dim 'arraySize' and 'i' but nevertheless you've earned yourself 500 points.
I don't fully understand how this works but I'm glad it does.
Cheers
Thanks for the points, just forgot to Dim the i ;-)
How it works, it is quiet simple:
The for loop scans for every element in the array, it select a random element in the array and swaps it with the current element.
How it works, it is quiet simple:
The for loop scans for every element in the array, it select a random element in the array and swaps it with the current element.
ASKER
Cheers.
Look out for my followup question because I'll no doubt run into further problems.
Thanks again!
Look out for my followup question because I'll no doubt run into further problems.
Thanks again!
j = Int(arraySize * Rnd)
does not produce a fair shuffle
j = Int(i* Rnd)
is better
does not produce a fair shuffle
j = Int(i* Rnd)
is better
http://www.vb-helper.com/howto_randomize_array.html
http://www.freevbcode.com/ShowCode.asp?ID=1174
http://www.mvps.org/vb/hardcore/html/shuffling.htm
I don't know which is the best, but I hope this is somewhat helpful.