Durstenfeld’s Algorithm Jumble Array
-
- PlatinumLounger
- Posts: 4931
- Joined: 31 Aug 2016, 09:02
Re: Durstenfeld’s Algorithm Jumble Array
UDFs is considered a ready-used solutions for such stuff so I prefer using them. I don't know if I am right or not, but I like using UDFs.
-
- Administrator
- Posts: 78558
- Joined: 16 Jan 2010, 00:14
- Status: Microsoft MVP
- Location: Wageningen, The Netherlands
-
- PlatinumLounger
- Posts: 4931
- Joined: 31 Aug 2016, 09:02
Re: Durstenfeld’s Algorithm Jumble Array
Suppose I have a range I would assign the range as parameter
JumbelArray(rng as range)
JumbelArray(rng as range)
-
- Administrator
- Posts: 78558
- Joined: 16 Jan 2010, 00:14
- Status: Microsoft MVP
- Location: Wageningen, The Netherlands
Re: Durstenfeld’s Algorithm Jumble Array
What should the return value be? A one-dimensional array or an array the same size as the input range?
Best wishes,
Hans
Hans
-
- PlatinumLounger
- Posts: 4931
- Joined: 31 Aug 2016, 09:02
Re: Durstenfeld’s Algorithm Jumble Array
the desired output would be an array the same size as the input range
-
- Administrator
- Posts: 78558
- Joined: 16 Jan 2010, 00:14
- Status: Microsoft MVP
- Location: Wageningen, The Netherlands
Re: Durstenfeld’s Algorithm Jumble Array
For example:
To use this as a UDF, you must confirm it with Ctrl+Shift+Enter to make it an array formula.
Code: Select all
Function ShuffleRange(rng As Range)
Dim n As Integer
Dim i As Integer
Dim j As Integer
Dim k As Integer
Dim nr As Integer
Dim nc As Integer
Dim rn As Integer
Dim Temp
With rng
n = .Cells.Count
nr = .Rows.Count
nc = .Columns.Count
End With
ReDim a(1 To n)
k = 1
For i = 1 To nr
For j = 1 To nc
a(k) = rng.Cells(i, j)
k = k + 1
Next j
Next i
k = 1
For i = 1 To nr
For j = 1 To nc
rn = WorksheetFunction.RandBetween(1, n - k + 1)
Temp = a(n - k + 1)
a(n - k + 1) = a(rn)
a(rn) = Temp
k = k + 1
Next j
Next i
ReDim output(1 To nr, 1 To nc)
k = 1
For i = 1 To nr
For j = 1 To nc
output(i, j) = a(k)
k = k + 1
Next j
Next i
ShuffleRange = output
End Function
You do not have the required permissions to view the files attached to this post.
Best wishes,
Hans
Hans
-
- PlatinumLounger
- Posts: 4931
- Joined: 31 Aug 2016, 09:02
Re: Durstenfeld’s Algorithm Jumble Array
Thank you very much for your patience my tutor
Best Regards
Best Regards