Generate 5000 records in 2 columns of random number that being unique

删除回忆录丶 提交于 2019-11-27 16:31:24

Here is a simple-minded approach using formulae. Whether it would be appropriate would depend on context.

First in the Formulas tab set calculation options to 'Manual'.

Put the following formula in a2:-

=RANDBETWEEN(1,100)

B is going to be a helper column. Put the following in B2:-

=RANDBETWEEN(1,99)

Column C is the second result that you want. Put the following in C2:-

=IF(B2<A2,B2,B2+1)

Pull the formulae down as required.

Each time you press 'Calculate Now', you will get a fresh set of random numbers.

However if you really need unique rows (every row to be different) you'd need a different approach - could generate a set of 4-digit numbers, split them into first and last pairs of digits and filter out ones where first and second were equal.

Generate the 4-digit number in A2:-

=RANDBETWEEN(1,9998)

Take the first two-digit number plus one in B2:-

=INT(A2/100)+1

Take the second 2-digit number plus one in C2:-

=MOD(A2,100)+1

Check for invalid numbers in D2:-

=OR(ISNUMBER(MATCH(A2,A$1:A1,0)),B2=C2)

Set up a running total of valid numbers in E2:-

=COUNTIF(D2:D$2,FALSE)

Here's how the second approach would look with checking for duplicate rows as well as duplicate numbers within a row. Note that you'd have to generate about 3,000 rows to get 2,500 distinct rows:-

Excel formulas do not perform loops until a condition has been met. Any 'loop' or array processing must have a defined number of cycles. Further, RAND and RANDBETWEEN are volatile formulas that will recalculate anytime the workbook goes through a calculation cycle.

In VBA this would look like the following.

Sub Random_2500_x_2()
    Dim rw As Long
    For rw = 1 To 2500
        Cells(rw, 1) = Int((100 - 1 + 1) * Rnd + 1)
        Cells(rw, 2) = Int((100 - 1 + 1) * Rnd + 1)
        Do Until Cells(rw, 2).Value <> Cells(rw, 1).Value
            Cells(rw, 2) = Int((100 - 1 + 1) * Rnd + 1)
        Loop
    Next rw
End Sub

First run this tiny macro:

Sub dural()
    Application.ScreenUpdating = False
    Application.Calculation = xlCalculationManual
    k = 1
    For i = 1 To 100
        For j = 1 To 100
            Cells(k, 1) = i
            Cells(k, 2) = j
            Cells(k, 3).Formula = "=rand()"
            k = k + 1
        Next j
    Next i
    Application.Calculation = xlCalculationManual
    Application.ScreenUpdating = True
End Sub

Then sort cols A,B,C by column C.
Then pick the first 5000 rows.


EDIT#1:

To remove cases in which the value in column A is the same as the value in column B use this macro instead:

Sub dural()
    Application.ScreenUpdating = False
    Application.Calculation = xlCalculationManual
    k = 1
    For i = 1 To 100
        For j = 1 To 100
            If i <> j Then
                Cells(k, 1) = i
                Cells(k, 2) = j
                Cells(k, 3).Formula = "=rand()"
                k = k + 1
            End If
        Next j
    Next i
    Application.Calculation = xlCalculationManual
    Application.ScreenUpdating = True
End Sub
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!