Generate list of numbers in random order using VBA

bluefeather8989

Active Member
Joined
Nov 20, 2009
Messages
327
Office Version
  1. 365
Platform
  1. Windows
I need VBA to make a list of numbers in A:A from 101 to 132 in random order. each number to be used only once
 

Excel Facts

Formula for Yesterday
Name Manager, New Name. Yesterday =TODAY()-1. OK. Then, use =YESTERDAY in any cell. Tomorrow could be =TODAY()+1.
VBA Code:
Function RandArrayNoRepeats(startNo As Long, endNo As Long)
    Dim i As Long, tot As Long, b
    tot = endNo - startNo + 1
    ReDim a(1 To tot, 1 To 2)
    For i = 1 To tot
        a(i, 2) = Rnd()
        a(i, 1) = startNo + i - 1
    Next
    b = Application.Sort(a, 2)
    ReDim Preserve b(1 To tot, 1 To 1)
    RandArrayNoRepeats = b
End Function

Book2
ABCD
1127101
2124102
3116103
4109104
5129105
6108106
7119107
8110108
9130109
10102110
11113111
12106112
13118113
14105114
15131115
16101116
17120117
18123118
19125119
20114120
21107121
22111122
23104123
24115124
25117125
26132126
27121127
28103128
29122129
30112130
31128131
32126132
33
Sheet3
Cell Formulas
RangeFormula
A1:A32A1=RandArrayNoRepeats(101,132)
C1:C32C1=SORT(A1#)
Dynamic array formulas.
 
Upvote 0
FWIW you could eliminate the b variable and the last part of the function could be achieved directly as follows

VBA Code:
Function RandArrayNoRepeats(startNo As Long, endNo As Long)
    Dim i As Long, tot As Long ', b
    tot = endNo - startNo + 1
    ReDim a(1 To tot, 1 To 2)
    For i = 1 To tot
        a(i, 2) = Rnd()
        a(i, 1) = startNo + i - 1
    Next
'    b = Application.Sort(a, 2)
'    ReDim Preserve b(1 To tot, 1 To 1)
    RandArrayNoRepeats = Application.Index(Application.Sort(a, 2), 0, 1)
End Function
 
Upvote 0
The easy swap VBA demonstration :​
VBA Code:
Sub Demo1()
    Dim V, L&, R&, S
        V = [ROW(101:132)]
    For L = UBound(V) To 2 Step -1
        R = Application.RandBetween(1, L)
        S = V(L, 1):  V(L, 1) = V(R, 1):  V(R, 1) = S
    Next
        [A1].Resize(UBound(V)).Value2 = V
End Sub
 
Upvote 0
Solution
The easy swap VBA demonstration
Personal opinion/feeling without proof, i think this method is less random than the others, but i don't know how to prove it.
Run that macro 1,000 or 1,000,000 times and check duplicates ???

Perhaps a question for the gurus here, with rnd(), it's better to use Randomize.
Is that also the case with RandBetween and for the new RandArray ?
 
Upvote 0
I have read a long time ago that the VBA Rnd function was the worse randomizer ever created​
but in many posts on many forums I often used it 'cause I was often under Excel 2003 version.​
In fact my demonstration is a swap shuffle method …​
Important to remind :​
Rnd must be initialized with the Randomize statement or each time the same 'random' sequence will be generated !​
No such issue using the native worksheet functions the reason why I use it or 'cause I was lazy ! ;) (The lazy the smarter ? :cool:)​
 
Upvote 0
The lazy the smarter ? :cool:
great !
The new RandArray (2021 & 365, not in your 2010 !) is better random and is specially made for lazy people.
Ask for an array of 1,000 elements random integer between 101 and 132 (with repetition), a fraction of a second later, you got them.

And now, back to the topicstarter, we 're hijacking him.
 
Upvote 0
The easy swap VBA demonstration :​
VBA Code:
Sub Demo1()
    Dim V, L&, R&, S
        V = [ROW(101:132)]
    For L = UBound(V) To 2 Step -1
        R = Application.RandBetween(1, L)
        S = V(L, 1):  V(L, 1) = V(R, 1):  V(R, 1) = S
    Next
        [A1].Resize(UBound(V)).Value2 = V
End Sub
Thanks this is what I need. :)
How do I change the location. I know i said A;A but what if I need to change that to a specific range
?
 
Last edited:
Upvote 0
You can change the range destination within the codeline located just after the Next codeline …​
 
Upvote 0
The easy swap VBA demonstration :​
VBA Code:
Sub Demo1()
    Dim V, L&, R&, S
        V = [ROW(101:132)]
    For L = UBound(V) To 2 Step -1
        R = Application.RandBetween(1, L)
        S = V(L, 1):  V(L, 1) = V(R, 1):  V(R, 1) = S
    Next
        [A1].Resize(UBound(V)).Value2 = V
End Sub
So, i spent a good little bit of time picking this apart to try to understand what was going on! When I had that "aha!" moment it made my day (I must be a nerd). Anyhow, thank you for sharing. It works just like I need it to.
 
Upvote 0

Forum statistics

Threads
1,221,507
Messages
6,160,219
Members
451,631
Latest member
coffiajoseph

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