VB6中连续使用Randomize对后续随机数出现概率影响测试

时间:2022-03-31 23:14:59
'程序的思路是将a,b分别给一个0到num-1的随机数,循环多次,观察(a,b)的所有组合出现的次数,用来反映其出现的概率
'测试结果表明连续使用Randomize,出现的(a,b)组合并不是等概率的

Option
Explicit Sub Cal(ByVal num As Integer) Dim a, b, i, j, k, n, c() As Long ReDim c(num * num - 1) For i = 1 To 500000 Randomize a = Int(Rnd * num) Randomize '此处注释后结果不同 b = Int(Rnd * num) n = 0 For j = 0 To num - 1 If a = j Then For k = 0 To num - 1 If b = k Then GoTo f End If n = n + 1 Next k End If n = n + num Next j f: c(n) = c(n) + 1 Next i For i = 0 To num * num - 1 Debug.Print c(i), Next i Debug.Print End Sub Private Sub Command1_Click() Call Cal(3) End Sub