Random generator with no duplicates

anwaee2

Board Regular
Joined
Sep 13, 2012
Messages
151
Office Version
  1. 2011
Platform
  1. MacOS
I have row C5:L5 and column B6:B15 that I would like a macro that has a random generator place numbers 0 thru 9 in each cell with no duplicates in the row and no duplicates in the column. Is this even possible?
 

Excel Facts

Using Function Arguments with nested formulas
If writing INDEX in Func. Arguments, type MATCH(. Use the mouse to click inside MATCH in the formula bar. Dialog switches to MATCH.
It can be with formulas:

Cell Formulas
RangeFormula
A6:A15,C4:L4C4=RAND()
C5:L5C5=RANK(C4,$C$4:$L$4)-1
B6:B15B6=RANK(A6,$A$6:$A$15)-1


-------------------------------------------
I also give you the option with macro:
VBA Code:
Sub random9()
  Dim arr As Variant
  Dim i As Long, x As Long, y As Long
  Randomize
  
  ReDim arr(0 To 9, 1 To 1)
  For i = 0 To UBound(arr)
    arr(i, 1) = i
  Next
  
  For i = 0 To UBound(arr)
    x = Int(UBound(arr) * Rnd + 1)
    y = arr(i, 1)
    arr(i, 1) = arr(x, 1)
    arr(x, 1) = y
  Next i
  Range("B6").Resize(UBound(arr)).Value = arr
  
  For i = 0 To UBound(arr)
    x = Int(UBound(arr) * Rnd + 1)
    y = arr(i, 1)
    arr(i, 1) = arr(x, 1)
    arr(x, 1) = y
  Next i
  Range("C5").Resize(1, UBound(arr)).Value = Application.Transpose(arr)
End Sub
 
Upvote 0
Thank you, this is exactly what I needed in a macro. One small problem, the macro works great except it does not put a number in cell L5 or B15, All other cells are as they should be, random numbers with no duplicates.
Again, thank you so much. I thought if it could be done, someone on Mr. Excel would find a way to do it.
 
Upvote 0
One small problem, the macro works great except it does not put a number in cell L5 or B15

Try the following macro, I only made a small adjustment:
Rich (BB code):
Sub random9()
  Dim arr As Variant
  Dim i As Long, x As Long, y As Long
  Randomize
 
  ReDim arr(0 To 9, 1 To 1)
  For i = 0 To UBound(arr)
    arr(i, 1) = i
  Next
 
  For i = 0 To UBound(arr)
    x = Int(UBound(arr) * Rnd + 1)
    y = arr(i, 1)
    arr(i, 1) = arr(x, 1)
    arr(x, 1) = y
  Next i
  Range("B6").Resize(UBound(arr) + 1).Value = arr
 
  For i = 0 To UBound(arr)
    x = Int(UBound(arr) * Rnd + 1)
    y = arr(i, 1)
    arr(i, 1) = arr(x, 1)
    arr(x, 1) = y
  Next i
  Range("C5").Resize(1, UBound(arr) + 1).Value = Application.Transpose(arr)
End Sub

----- --
 
Upvote 0
Solution
There is another option, using only one loop 😉

VBA Code:
Sub random9b()
  Dim ar1 As Variant, ar2 As Variant
  Dim i As Long, x1 As Long, x2 As Long, y1 As Long, y2 As Long
  Randomize
  
  ar1 = [row(1:10)]
  ar1(10, 1) = 0
  ar2 = [row(1:10)]
  ar2(10, 1) = 0
  
  For i = 1 To UBound(ar1)
    x1 = Int(UBound(ar1) * Rnd + 1)
    x2 = Int(UBound(ar2) * Rnd + 1)
    y1 = ar1(i, 1)
    y2 = ar2(i, 1)
    ar1(i, 1) = ar1(x1, 1)
    ar1(x1, 1) = y1
    ar2(i, 1) = ar2(x2, 1)
    ar2(x2, 1) = y2
  Next i
  Range("B6").Resize(UBound(ar1)).Value = ar1
  Range("C5").Resize(1, UBound(ar2)).Value = Application.Transpose(ar2)
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,594
Messages
6,120,436
Members
448,964
Latest member
Danni317

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