我正在尝试创建一个不会重复的随机数列表,然后我想获取这些数字并生成多个都是随机数的列表。
for example
for cells A1-A10 i want random numbers from 1-100
and then for A11-20 I want random numbers from 1-100
and then for A21 - A30 I want random number from 1-100
通过仍在学习的MOD和OFFSET函数,可以拖拽并重复此操作。
我尝试生成数字
=RANDBETWEEN(1,100)
但是会产生重复。
我也尝试过 放
Rand()
G1中的并生成随机数 然后使用
=RANK.EQ(G1,$G$1:$G$100)
以及:
=INDEX($H$1:$H$100, RANK(G1,$G$1:$G$100), 1)
这是更好的方法,不会产生重复,但是由于我需要多个列表,所以发生的事情是每个列表都具有完全相同的数据,因为代码只是复制了相同的引用。
例如:我得到的是A1-10
A1 = 65
A2 = 54
A3 = 23
A4 = 31
A10= 23
然后我尝试重复A11的代码-A 20 但它会产生相同的值。
即使我使用
=INDEX($H$1:$H$50, RANK.EQ(G2, $G$1:$G$100) + COUNTIF($G$1:G2, G2) - 1, 1)
然后我有
=INDEX($H$1:$H$100, RANK(G1,$G$1:$G$100), 1)
在两个不同的行中,它们产生完全相同的随机数
所以我认为我需要每次都随机化的东西。
感谢您的帮助
答案 0 :(得分:1)
尝试以下VBA宏:
Sub vRandom()
Dim mn As Long, mx As Long, samples As Long
Dim times As Long, arr1(), t As Long, s As Long
Dim k As Long
mn = 1
mx = 100
samples = 10
times = 3
k = 1
ReDim arr1(mn To mx)
For s = mn To mx
arr1(s) = s
Next s
For t = 1 To times
Call Shuffle(arr1)
For s = 1 To samples
Cells(k, 1) = arr1(mn + s - 1)
k = k + 1
Next s
Next t
End Sub
Public Sub Shuffle(InOut() As Variant)
Dim i As Long, J As Long
Dim tempF As Double, Temp As Variant
Hi = UBound(InOut)
Low = LBound(InOut)
ReDim Helper(Low To Hi) As Double
Randomize
For i = Low To Hi
Helper(i) = Rnd
Next i
J = (Hi - Low + 1) \ 2
Do While J > 0
For i = Low To Hi - J
If Helper(i) > Helper(i + J) Then
tempF = Helper(i)
Helper(i) = Helper(i + J)
Helper(i + J) = tempF
Temp = InOut(i)
InOut(i) = InOut(i + J)
InOut(i + J) = Temp
End If
Next i
For i = Hi - J To Low Step -1
If Helper(i) > Helper(i + J) Then
tempF = Helper(i)
Helper(i) = Helper(i + J)
Helper(i + J) = tempF
Temp = InOut(i)
InOut(i) = InOut(i + J)
InOut(i + J) = Temp
End If
Next i
J = J \ 2
Loop
End Sub
该代码生成 3 个项目块(每个块10个项目)。
内没有重复项,但在 块之间可能存在重复项。