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

后端 未结 3 2033
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-12-04 04:14

How I can generate 5000 records in 2 columns of random numbers between 1 and 100 that being unique.

For example:

 A            B
----------------
 1          


        
相关标签:
3条回答
  • 2020-12-04 04:53

    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.

    enter image description here


    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
    
    0 讨论(0)
  • 2020-12-04 04:55

    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
    
    0 讨论(0)
  • 2020-12-04 05:01

    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.

    enter image description here

    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:-

    enter image description here

    0 讨论(0)
提交回复
热议问题