选项 概率
甲 3%
乙 7%
丙 17%
丁 13%
戊 17%
己 20%
庚 23%
如上所示,7种选项的概率分布,和为100%,用VBA要怎么判断呢?
比如要模拟抽取100次的结果!
vba概率抽取问题
答案:3 悬赏:60 手机版
解决时间 2021-01-30 14:26
- 提问者网友:轻浮
- 2021-01-29 20:48
最佳答案
- 五星知识达人网友:一把行者刀
- 2021-01-29 21:47
主程序就是这样,你可以扩展着修改
Sub rdd()
t = Rnd() * 100
Select Case t
Case Is < 3
msg_t = "甲"
Case Is < 10
msg_t = "乙"
Case Is < 27
msg_t = "丙"
Case Is < 40
msg_t = "丁"
Case Is < 57
msg_t = "戊"
Case Is < 77
msg_t = "己"
Case Is < 100
msg_t = "庚"
End Select
MsgBox msg_t
End Sub
Sub rdd()
t = Rnd() * 100
Select Case t
Case Is < 3
msg_t = "甲"
Case Is < 10
msg_t = "乙"
Case Is < 27
msg_t = "丙"
Case Is < 40
msg_t = "丁"
Case Is < 57
msg_t = "戊"
Case Is < 77
msg_t = "己"
Case Is < 100
msg_t = "庚"
End Select
MsgBox msg_t
End Sub
全部回答
- 1楼网友:荒野風
- 2021-01-30 00:07
代码如下:
Sub aa()
Application.ScreenUpdating = False
Dim iRnd%, str$
Dim arr As String
Randomize
For i = 1 To 100
iRnd = Rnd * 100
If iRnd < 3 Then
str = "甲"
ElseIf iRnd < 10 Then
str = "乙"
ElseIf iRnd < 27 Then
str = "丙"
ElseIf iRnd < 40 Then
str = "丁"
ElseIf iRnd < 57 Then
str = "戊"
ElseIf iRnd < 77 Then
str = "己"
Else
str = "庚"
End If
arr = arr & " " & str
Next
Cells(1, 1).Resize(100, 1) = Application.Transpose(Split(Right(arr, Len(arr) - 1), " "))
Application.ScreenUpdating = True
End Sub上面代码是直接生成100个数据,经测试,勉强符合所要求。但还是有一定偏差。当然,生成的数据越多,越符合你的要求。
原理:
根据随机数的概率,判断所生成的数。
- 2楼网友:迟山
- 2021-01-29 22:47
代码如下:
Sub aa()
Application.ScreenUpdating = False
Dim iRnd%, str$
Dim arr As String
Randomize
For i = 1 To 100
iRnd = Rnd * 100
If iRnd < 3 Then
str = "甲"
ElseIf iRnd < 10 Then
str = "乙"
ElseIf iRnd < 27 Then
str = "丙"
ElseIf iRnd < 40 Then
str = "丁"
ElseIf iRnd < 57 Then
str = "戊"
ElseIf iRnd < 77 Then
str = "己"
Else
str = "庚"
End If
arr = arr & " " & str
Next
Cells(1, 1).Resize(100, 1) = Application.Transpose(Split(Right(arr, Len(arr) - 1), " "))
Application.ScreenUpdating = True
End Sub上面代码是直接生成100个数据,经测试,勉强符合所要求。但还是有一定偏差。
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯