Random number generation without repeating across same row or column?

Simtech76

New Member
Joined
Nov 24, 2011
Messages
10
Hello,

Been trying to think of a way to get random numbers 1 to 36 generated without repeating in the same column, however also getting it to perform the same opertaion in 9 more columns (B:J) without the same number appearing in the same row.

example

1 2 5 4
2 5 3 1
3 4 1 5
4 1 2 3
5 3 4 2

Unsure if this is possible, have created a basic script for random number generation but have no clue how to expand across columns.

Thanks, Simon
 

Excel Facts

Highlight Duplicates
Home, Conditional Formatting, Highlight Cells, Duplicate records, OK to add pink formatting to any duplicates in selected range.
Just for fun, you might be interested in trying this code on a blank worksheet.
Code:
Sub just4fun()
ActiveSheet.UsedRange.ClearContents
Dim n As Long, j As Long, x As Long, y As Long
Dim a(), u()
n = InputBox("How many?")
ReDim a(1 To n, 1 To 2)
For i = 1 To n
    a(i, 1) = i
    a(i, 2) = i
Next i
For j = 1 To 2: For i = 1 To n
    x = Int(Rnd * (n - i + 1)) + i
    y = a(x, j)
    a(x, j) = a(i, j)
    a(i, j) = y
Next i, j

ReDim u(1 To n, 1 To n)
For j = 1 To n
    For i = 1 To n
        y = a(i, 1) + a(j, 1) - 1
    If y > n Then y = y - n
    u(a(i, 2), j) = y
    Next i
Next j
Cells(1).Resize(n, n) = u
End Sub
 
Upvote 0
Just for fun, you might be interested in trying this code on a blank worksheet.
Code:
Sub just4fun()
ActiveSheet.UsedRange.ClearContents
Dim n As Long, j As Long, x As Long, y As Long
Dim a(), u()
n = InputBox("How many?")
ReDim a(1 To n, 1 To 2)
For i = 1 To n
    a(i, 1) = i
    a(i, 2) = i
Next i
For j = 1 To 2: For i = 1 To n
    x = Int(Rnd * (n - i + 1)) + i
    y = a(x, j)
    a(x, j) = a(i, j)
    a(i, j) = y
Next i, j

ReDim u(1 To n, 1 To n)
For j = 1 To n
    For i = 1 To n
        y = a(i, 1) + a(j, 1) - 1
    If y > n Then y = y - n
    u(a(i, 2), j) = y
    Next i
Next j
Cells(1).Resize(n, n) = u
End Sub

The members of this forum never cease to amaze me, thank you.
 
Upvote 0
Just for fun, you might be interested in trying this code on a blank worksheet.
Code:
Sub just4fun()
ActiveSheet.UsedRange.ClearContents
Dim n As Long, j As Long, x As Long, y As Long
Dim a(), u()
n = InputBox("How many?")
ReDim a(1 To n, 1 To 2)
For i = 1 To n
    a(i, 1) = i
    a(i, 2) = i
Next i
For j = 1 To 2: For i = 1 To n
    x = Int(Rnd * (n - i + 1)) + i
    y = a(x, j)
    a(x, j) = a(i, j)
    a(i, j) = y
Next i, j

ReDim u(1 To n, 1 To n)
For j = 1 To n
    For i = 1 To n
        y = a(i, 1) + a(j, 1) - 1
    If y > n Then y = y - n
    u(a(i, 2), j) = y
    Next i
Next j
Cells(1).Resize(n, n) = u
End Sub


Thanks for your efforts. I'm interested in a random number generator that works the same as the one in the above thread, but with a different twist, if possible. The Cash 3 lottery game has digits from 0-9 for 3 positions. If you can create a random generator that produces values 10-30 for a cash 3 game and 10-40 for a play 4 game, would be very much appreciated. For the Cash 3, you would have 30 values ranging from 10-30. For the Play 4, you would have 40 values ranging from 10-40. Is this possible to do? no duplicates in either columns or rows.
 
Last edited:
Upvote 0
maybe like this?
gives 4 randomly selected, nonrepeated, numbers from 10 to 40
the numbers are sorted for you but easy to leave out the sorting feature if you don't want this
Code:
Sub xyz()
Dim b(30) As Boolean, x&, k&, c&
Do
    x = Int(Rnd * 31)
    If Not b(x) Then k = k + 1:  b(x) = True
Loop Until k = 4
For x = 0 To 30
   If b(x) Then c = c + 1: Cells(c, 1) = x + 10
Next x
End Sub
:):)
 
Upvote 0
maybe like this?
gives 4 randomly selected, nonrepeated, numbers from 10 to 40
the numbers are sorted for you but easy to leave out the sorting feature if you don't want this
Code:
Sub xyz()
Dim b(30) As Boolean, x&, k&, c&
Do
    x = Int(Rnd * 31)
    If Not b(x) Then k = k + 1:  b(x) = True
Loop Until k = 4
For x = 0 To 30
   If b(x) Then c = c + 1: Cells(c, 1) = x + 10
Next x
End Sub
:):)


That doesn't work. Not even sure it can be done. The output would look like this, but the values in each column and row, would range from 10 to 39.
Excel Workbook
ABCDEFGHIJ
112345678910
283527281123221210
31914168922432321
414911341729281816
52722241617301211129
694628291224231311
CASH 3
Excel 2007
 
Upvote 0
That doesn't work. Not even sure it can be done. The output would look like this, but the values in each column and row, would range from 10 to 39.
It doesn't work coz of great ambiguity about just what it was that you wanted to "work".

If you want to select 30 non-repeating integers from 10 to 30, then no, that's not possible.

Otherwise, since I'm not a mind-reader, perhaps you could be clearer about just what you're looking for?

Is it some 10 by 6 table, with 1 to 10 along the top and 50 randomized integers underneath?

So why should you want some VBA to do this?
 
Upvote 0
That's a terrific solution to the original problem, Mirabeau. Very elegant, no brute force required. That's why I love this board, I often learn something new.
:pray:
 
Upvote 0
That's a terrific solution to the original problem, Mirabeau. Very elegant, no brute force required. That's why I love this board, I often learn something new.
:pray:
Thanks for the feedback gardnertoo. I really appreciate that. :)

That code I wrote a year or so back. It was just taking up space on my hard drive so I thought someone else may as well get some interest or possible further use from it.

(As you probably know) it's a type of construct called a "Latin Square". Has use in (statistical) experimental design and probably other applications as well.
 
Upvote 0
It doesn't work coz of great ambiguity about just what it was that you wanted to "work".

If you want to select 30 non-repeating integers from 10 to 30, then no, that's not possible.

Otherwise, since I'm not a mind-reader, perhaps you could be clearer about just what you're looking for?

Is it some 10 by 6 table, with 1 to 10 along the top and 50 randomized integers underneath?

So why should you want some VBA to do this?

Thanks for you efforts.
 
Upvote 0

Forum statistics

Threads
1,214,833
Messages
6,121,858
Members
449,052
Latest member
Fuddy_Duddy

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