Shuffle an Array?

User Name

Spammer
Joined
Aug 10, 2010
Messages
182
I'd like to build an array with the days of the month randomly sorted. i.e. Neglecting leap year I'd like 1 to 28 for February, 1 to 30 for June or 1 to 31 for January randomly sorted.

Any ideas?
 

Excel Facts

When they said...
When they said you are going to "Excel at life", they meant you "will be doing Excel your whole life".
In an convenient column, enter =RAND() and copy down. Sort by that.
 
Upvote 0
Ahhh....Thanks but I need to shuffle an array in VBA. I'd use rnd to fill up one column but I don't know how to sort by this column.
 
Upvote 0
Well then ...

Code:
Sub x()
    Dim aiFeb() As Long
    
    aiFeb = aiRandLong(1, 28)
End Sub
 
Public Function aiRandLong(iMin As Long, _
                           iMax As Long, _
                           Optional ByVal n As Long = -1, _
                           Optional bVolatile As Boolean = False) As Long()
    ' UDF or VBA
 
    ' Adapted from Chip Pearson at [URL]http://www.cpearson.com/excel/RandomNumbers.aspx[/URL]
    ' Returns a 1-based array of n unique Longs between iMin and iMax inclusive

    Dim aiSrc()     As Long
    Dim aiOut()     As Long
    Dim iSrc        As Long
    Dim iOut        As Long
    Dim iTop        As Long
 
    If bVolatile Then Application.Volatile
 
    If n = -1 Then n = iMax - iMin + 1
    If iMin > iMax Or n > (iMax - iMin + 1) Or n < 1 Then Exit Function
 
    ReDim aiSrc(iMin To iMax)
    ReDim aiOut(1 To n)
 
    ' init iSrc
    For iSrc = iMin To iMax
        aiSrc(iSrc) = iSrc
    Next iSrc
 
    iTop = iMax
    For iOut = 1 To n
        ' pick a number between 1 and iTop, swap with iTop, decrement iTop
        iSrc = Int((iTop - iMin + 1) * Rnd) + iMin
        aiOut(iOut) = aiSrc(iSrc)
        aiSrc(iSrc) = aiSrc(iTop)
        iTop = iTop - 1
    Next iOut
 
    aiRandLong = aiOut
End Function
 
Upvote 0
Try this function.
Code:
Function scrambledArray(unscrambledArray As Variant) As Variant
    Dim workingArray As Variant
    Dim Size As Long, i As Long, ii As Long
    Dim randIndex As Long, temp As Variant
    
    workingArray = unscrambledArray
    Size = UBound(workingArray) - LBound(workingArray) + 1
    
    
    For i = LBound(workingArray) To UBound(workingArray)
        Randomize
        randIndex = Int(Size * Rnd()) + LBound(workingArray)
        
        temp = workingArray(randIndex)
        workingArray(randIndex) = workingArray(i)
        workingArray(i) = temp
    Next i
    
    scrambledArray = workingArray
End Function

Sub test()
    Dim i As Long
    For i = 1 To 10
        Range("A65536").End(xlUp).Offset(1, 0) = Join(scrambledArray(Array(1, 2, 3, 4, 5)))
    Next i
End Sub
 
Upvote 0
Thank you for the suggestions. I'm stumped by them at the moment but they will probably make sense in the morning. One question, why is this code written as a function?

HTML:
CDF_Step = 1
Day = 1
Dim KT_Array(0 To 31) As Variant
Dim Number_Array(0 To 31) As Variant
        
Do While CDF_Step < Month_step
        
'Townsend's solution for daily Kt values - S1272
KT_Day = 1 / Gamma_X * Log((1 - CDF_Step / Month_step) * _
e ^ (Gamma_X * 0.05) + CDF_Step / Month_step * e ^ (Gamma_X * KT_Max))
            
KT_Array(Day) = KT_Day
Number_Array(Day) = Day
Day = Day + 1
CDF_Step = CDF_Step + 2
            
Loop

The array I want to scramble is the Number_Array. Then I want to sort the KT_Array in the same order as the scrambled Number_Array.
 
Upvote 0
If you just want to shuffle the day numbers from 1 to 30 (or 1 to 28, or 1 to 31) with a straight sub and without a helper column, then there's a number of ways, e.g.
Code:
Sub shuffle()
Dim b() As Boolean, a, n&, x&, k&
n = 30
ReDim b(1 To n), a(1 To n, 1 To 1)
Do
    x = Int(Rnd * n) + 1
    If Not b(x) Then
        k = k + 1
        a(k, 1) = x
        b(x) = True
    End If
Loop Until k = n
[c1].Resize(n) = a
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,586
Messages
6,179,719
Members
452,939
Latest member
WCrawford

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top