第1个回答 2012-07-07
VB 随机数频率控制
'添加 Command1
'0-59为小数, 60以上为大数
'i 是要运行几次
'jj 是得到的随机数
'kk 是控制每10个数则换行显示
'minnum 是统计小数出现的次数
'getrnd 本次得到的随机数是否有效
Dim countrnd%, i%, jj%, kk%, minnum%, getrnd As Boolean
Private Sub Form_Load()
Me.AutoRedraw = True
End Sub
Private Sub Command1_Click()
Randomize
getrnd = False
countrnd = 0: kk = 0: minnum = 0
Me.Cls
For i = 1 To 100
Do
jj = Int(Rnd * 100)
If jj >= 60 Then
Me.ForeColor = QBColor(0)
Print jj;
kk = kk + 1
If kk Mod 10 = 0 Then Print
Exit Do
End If
If getrnd Then
Me.ForeColor = QBColor(12)
Print jj;
kk = kk + 1
minnum = minnum + 1
If kk Mod 10 = 0 Then Print
getrnd = False
countrnd = 0
Exit Do
End If
Loop
countrnd = countrnd + 1
If countrnd > 6 Then getrnd = True '使用6来控制出现小数的频率,数字越大则出现频率越少
Next i
MsgBox "小数出现 " & CStr(minnum) & " 次"
End Sub