Solved

How do you randomly shuffle an array

Posted on 2004-08-24
9
343 Views
Last Modified: 2011-09-20
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
0
Comment
Question by:pgilfeather
9 Comments
 

Expert Comment

by:MalteseDuck
ID: 11882423
Here are some links with different algorithms you can try:

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.
0
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 11882733
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
0
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 11882823
Oops! Detected some bug:

           j = Int((arraySize-1) * Rnd)+LBound(MyArray)

 
0
Simplifying Server Workload Migrations

This use case outlines the migration challenges that organizations face and how the Acronis AnyData Engine supports physical-to-physical (P2P), physical-to-virtual (P2V), virtual to physical (V2P), and cross-virtual (V2V) migration scenarios to address these challenges.

 

Author Comment

by:pgilfeather
ID: 11883020
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
0
 
LVL 55

Accepted Solution

by:
Jaime Olivares earned 500 total points
ID: 11883149
Currently do you see your ordered array?
If so just merge my code with yours:

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

Dim MyArray(4) As Integer
Dim arraySize, j as Integer
Dim temp as Integer   ' Must be the same type as array elements

MyArray(0) = 6546
MyArray(1) = 6547
MyArray(2) = 6548
MyArray(3) = 6549
MyArray(4) = 6550

        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

lstNumbers.DataSource = MyArray
lstNumbers.DataBind()      
End Sub

If you have any error while compiling, please tell.
0
 

Author Comment

by:pgilfeather
ID: 11883221
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
0
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 11883297
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.
 
0
 

Author Comment

by:pgilfeather
ID: 11883349
Cheers.

Look out for my followup question because I'll no doubt run into further problems.

Thanks again!
0
 
LVL 84

Expert Comment

by:ozo
ID: 11889727
         j = Int(arraySize * Rnd)
does not produce a fair shuffle
          j = Int(i* Rnd)
is better
0

Featured Post

Does Powershell have you tied up in knots?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Here we come across an interesting topic of coding guidelines while designing automation test scripts. The scope of this article will not be limited to QTP but to an overall extent of using VB Scripting for automation projects. Introduction Now…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…

773 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question