Randomise a List but spread results out so they don't appear near or straight after itself

Dazzybeeguy

Board Regular
Joined
Jan 6, 2022
Messages
121
Office Version
  1. 365
  2. 2010
Platform
  1. Windows
I have a list in A1:A213 that contains 14 distinct values.
I am using =SORTBY(A1:A213,RANDARRAY(COUNTA(A1:A213))) to randomise that list which it does.


L
M
R
SWM
LM
LMS
EAM
SNM
H
W
WS
LMN
MH
IO

My issue is that I want an even spread, I get the same value next to or very near to itself and was hoping there was something I could do to mitigate or eradicate this.

Column A Full List Column B with =SORTBY(A1:A200,RANDARRAY(COUNTA(A1:A213)))


LSWM
MH
MWS
RH
SWMR
LMM
LMSH
EAMSNM
MH
LMLM
SNMSWM
LMSR
SWMR
RM
HLMN
EAMW
WLM
WSM
LMSW
MH
WH
HH
SWMIO
WSEAM
SNMLMN
MR
HH
WSW
SWMR
SWMH
LMNIO
HEAM
SNMR
HM
LMSLMN
RH
HM
RLM
HEAM
MHR
HH
RH
MM
WH
MH
MH
HWS
EAMLMN
HLM
LMNW
SWMM
MH
MHR
MM
WWS
LMNLMS
HLM
SNMR
REAM
LMNSWM
WSR
SNMEAM
HH
SWMMH
HM
MEAM
LMNMH
HH
LMSH
WH
WSLMN
LMNR
LMSLMS
EAMM
MSWM
RH
LMSR
LMNM
SWMH
HH
MM
WM
MHM
LMEAM
WSEAM
HM
MWS
HMH
RSWM
EAMM
LMNM
HSWM
RH
WSL
SNMH
SWMEAM
SWMSNM
RM
HSWM
WLMS
EAMR
SWMEAM
EAMSNM
MHR
SNMM
SNMLMS
LMEAM
LMNM
RM
HLMN
MHLM
IOM
HLMN
LMSH
EAMW
RW
HLMS
RMH
HLM
WLMS
RW
LMSM
LMSM
RH
HSWM
EAMSNM
WEAM
MLMS
HM
WLMN
HLMS
SNMH
HLMS
RMH
RM
MWS
LMNR
MSNM
RH
MSNM
MWS
EAMMH
LMSR
SNMLM
RSNM
WEAM
MHW
WSW
SNMH
MH
MSNM
HWS
WSWS
HEAM
WSM
EAMSWM
MLMN
WSSNM
HH
HH
MM
LMM
SNMLM
HIO
LMSH
HLMS
IOSNM
LMH
RR
IOW
EAMH
HLM
LMNEAM
EAMWS
MHLMS
LMR
MLM
MM
EAMLM
HH
LMLMN
MR
HH
WH
LMLMN
LMSNM
HMH
SNMR
LMNWS
MHH
LMM
MM
LMSR
MHWS
HSNM
EAMSWM
LMNLMS
HLMS
LMSWM
MR
WSW
LMSM
MLMS
HSWM
LMSMH
MH
SWMLMS
MWS
HLMN
RLMN
MW
RMH
MSNM
 
And in your output: do you want the same number of repetitions of single values, so if in column A you have


Ljust once
M33 times
R22 times
and so on?

the same number of each single entry shall be in column B?

If so, I suspect its not possible or very complicated with formulas. while with VBA it can be probably achieved quite easily.

So: is VBA solution acceptable?


And:
HH

is obviously unwanted situation.
How about
EAMW
WLM

or
HLMS
IOSNM
LMH
 
Last edited:
Upvote 0
I have a list in A1:A213 that contains 14 distinct values.
I am using =SORTBY(A1:A213,RANDARRAY(COUNTA(A1:A213))) to randomise that list which it does.


L
M
R
SWM
LM
LMS
EAM
SNM
H
W
WS
LMN
MH
IO

My issue is that I want an even spread, I get the same value next to or very near to itself and was hoping there was something I could do to mitigate or eradicate this.

Column A Full List Column B with =SORTBY(A1:A200,RANDARRAY(COUNTA(A1:A213)))


LSWM
MH
MWS
RH
SWMR
LMM
LMSH
EAMSNM
MH
LMLM
SNMSWM
LMSR
SWMR
RM
HLMN
EAMW
WLM
WSM
LMSW
MH
WH
HH
SWMIO
WSEAM
SNMLMN
MR
HH
WSW
SWMR
SWMH
LMNIO
HEAM
SNMR
HM
LMSLMN
RH
HM
RLM
HEAM
MHR
HH
RH
MM
WH
MH
MH
HWS
EAMLMN
HLM
LMNW
SWMM
MH
MHR
MM
WWS
LMNLMS
HLM
SNMR
REAM
LMNSWM
WSR
SNMEAM
HH
SWMMH
HM
MEAM
LMNMH
HH
LMSH
WH
WSLMN
LMNR
LMSLMS
EAMM
MSWM
RH
LMSR
LMNM
SWMH
HH
MM
WM
MHM
LMEAM
WSEAM
HM
MWS
HMH
RSWM
EAMM
LMNM
HSWM
RH
WSL
SNMH
SWMEAM
SWMSNM
RM
HSWM
WLMS
EAMR
SWMEAM
EAMSNM
MHR
SNMM
SNMLMS
LMEAM
LMNM
RM
HLMN
MHLM
IOM
HLMN
LMSH
EAMW
RW
HLMS
RMH
HLM
WLMS
RW
LMSM
LMSM
RH
HSWM
EAMSNM
WEAM
MLMS
HM
WLMN
HLMS
SNMH
HLMS
RMH
RM
MWS
LMNR
MSNM
RH
MSNM
MWS
EAMMH
LMSR
SNMLM
RSNM
WEAM
MHW
WSW
SNMH
MH
MSNM
HWS
WSWS
HEAM
WSM
EAMSWM
MLMN
WSSNM
HH
HH
MM
LMM
SNMLM
HIO
LMSH
HLMS
IOSNM
LMH
RR
IOW
EAMH
HLM
LMNEAM
EAMWS
MHLMS
LMR
MLM
MM
EAMLM
HH
LMLMN
MR
HH
WH
LMLMN
LMSNM
HMH
SNMR
LMNWS
MHH
LMM
MM
LMSR
MHWS
HSNM
EAMSWM
LMNLMS
HLMS
LMSWM
MR
WSW
LMSM
MLMS
HSWM
LMSMH
MH
SWMLMS
MWS
HLMN
RLMN
MW
RMH
MSNM
Does this give you want you need?

Set the variables up in the Main procedure to call the subRandomList procedure.

Randomise a List.xlsm
ABC
1LWS
2MR
3RLMN
4SWMR
5LMIO
6LMSL
7EAMLMN
8SNMLMS
9HEAM
10WM
11WSR
12LMNL
13MHWS
14IOSNM
15SWM
16EAM
17WS
18LMS
19MH
20WS
21M
22H
23W
24L
25EAM
26LMS
27SWM
28IO
29LMN
30W
31LMS
32WS
33SWM
34LM
35EAM
36IO
37M
38W
39LM
40M
41R
42M
43EAM
44IO
45SNM
46EAM
47IO
48SWM
49LMS
50SWM
51SNM
52M
53SNM
54IO
55SNM
56MH
57W
58EAM
59W
60L
61WS
62W
63EAM
64R
65SWM
66LM
67WS
68L
69SNM
70WS
71LMN
72LM
73IO
74LMN
75W
76MH
77LMS
78M
79IO
80LMN
81W
82LMS
83L
84R
85SNM
86LMS
87M
88SWM
89W
90LMN
91EAM
92R
93MH
94LMS
95LM
96WS
97SWM
98EAM
99SWM
100MH
101H
102LMS
103MH
104H
105IO
106SNM
107LM
108MH
109SWM
110R
111LM
112L
113WS
114LMN
115SWM
116W
117LMS
118LMN
119WS
120EAM
121M
122LMS
123LM
124W
125LM
126LMN
127R
128H
129IO
130SWM
131IO
132M
133IO
134H
135MH
136H
137SWM
138MH
139M
140EAM
141WS
142SWM
143LMS
144SNM
145SWM
146H
147R
148M
149MH
150M
151W
152MH
153SWM
154IO
155LMN
156EAM
157WS
158LMN
159LMS
160R
161LM
162LMS
163R
164H
165M
166R
167W
168SNM
169R
170IO
171EAM
172L
173LMS
174LM
175WS
176MH
177LMN
178R
179M
180L
181R
182LMS
183R
184IO
185MH
186H
187WS
188R
189SWM
190L
191LMN
192WS
193LMN
194LM
195EAM
196SNM
197LMN
198SNM
199R
200LMS
201H
202LM
203SNM
204LMS
205H
206L
207SNM
208R
209IO
210LMN
211LMS
212LMN
213IO
214
Sheet1


VBA Code:
Private Sub Main()

  ' Range("A1:A14") - List of values.
  ' 231 - Number of items in the results list.
  ' Range("B1") - Position on sheet of results list.

  Call subRandomList(Range("A1:A14"), 213, Range("B1"))

End Sub

Public Sub subRandomList(rng As Range, intSize As Integer, rngPosition As Range)
Dim i As Integer
Dim arr() As Variant
Dim arrResult() As String
Dim strString As String
Dim strLast As String
Dim intNumber As Integer
Dim s As String
Dim intRow As Integer

  arr = rng.Value
  
  strLast = arr(1, 1)
      
  For i = 1 To intSize + 1
  
    Do While True
      
      intNumber = Int((UBound(arr) - 1 + 1) * Rnd + 1)
      
      If strLast <> arr(intNumber, 1) Then
        strString = strString & arr(intNumber, 1) & ","
        Exit Do
      End If
            
    Loop
        
    strLast = arr(intNumber, 1)
      
  Next i
  
  arrResult = Split(Left(strString, Len(strString) - 1), ",")
      
  rngPosition.Resize(UBound(arrResult), 1).Value = Application.Transpose(arrResult)
    
End Sub
 
Upvote 0

Forum statistics

Threads
1,226,908
Messages
6,193,611
Members
453,810
Latest member
Gks77117

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