Pagebanner

ThreeWave Shuffle Array

This page describes code to shuffle an array into random order.
ShortFadeBar

SectionBreak

In various applications, you may find in useful or necessary to randomize an array. That is, to reorder the elements in random order. This page describes to VBA procedures to do this. The first procedure, ShuffleArray, takes an input array and returns a new array containing the elements of the input array in random order. The contents and order of the input array are not modified. The second procedure, ShuffleArrayInPlace, randomizes an input array, modifying the contents and order of the input array. This procedure does not return a value.

The Code

The code for both procedures is shown below. You can download a module file here.

Function ShuffleArray(InArray() As Variant) As Variant()
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' ShuffleArray
' This function returns the values of InArray in random order. The original
' InArray is not modified.
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
    Dim N As Long
    Dim L As Long
    Dim Temp As Variant
    Dim J As Long
    Dim Arr() As Variant
    
    
    Randomize
    L = UBound(InArray) - LBound(InArray) + 1
    ReDim Arr(LBound(InArray) To UBound(InArray))
    For N = LBound(InArray) To UBound(InArray)
        Arr(N) = InArray(N)
    Next N
    For N = LBound(InArray) To UBound(InArray)
        J = Int((UBound(InArray) - LBound(InArray) + 1) * Rnd + LBound(InArray))
        If N <> J Then
            Temp = Arr(N)
            Arr(N) = Arr(J)
            Arr(J) = Temp
        End If
    Next N
    ShuffleArray = Arr
End Function

Sub ShuffleArrayInPlace(InArray() As Variant)
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' ShuffleArrayInPlace
' This shuffles InArray to random order, randomized in place.
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
    Dim N As Long
    Dim L As Long
    Dim Temp As Variant
    Dim J As Long
   
    Randomize
    L = UBound(InArray) - LBound(InArray) + 1
    For N = LBound(InArray) To UBound(InArray)
        J = Int((UBound(InArray) - LBound(InArray) + 1) * Rnd + LBound(InArray))
        If N <> J Then
            Temp = InArray(N)
            InArray(N) = InArray(J)
            InArray(J) = Temp
        End If
    Next N
End Sub

You can download a module file here.

This page last updated: 31-July-2008

Created by Chip Pearson at Pearson Software Consulting, LLC
Email: chip@cpearson.com Before emailing me, please read this page
http://www.cpearson.com/Excel/ShuffleArray.aspx
Copyright © 1997 - 2009, Charles H. Pearson

Submit bug information or errors on the Bug And Error Report Page.



 


sectionbreak

Essential Tools For Developers

Add-in Express, true RAD tool for developing Office extensions


Essential Tools For Financial Analysts And Accounting Professionals

  
Ready


Advertise Your Product On This Site


SectionBreak