Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
625 views
in Technique[技术] by (71.8m points)

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

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

For example:

 A            B
----------------
 1            98
 1            23
 37           98
 6            56
 93           18
 .            .
 .            .
 .            .

thanks

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...