我目前正在处理一个Excel文件,该文件存储英文单词及其在土耳其语中的含义。我所做的是,Excel随机选择一个单词并显示我,我点击确定然后它显示了该单词的含义。这很好用。问题是,Excel中有587个单词,但我觉得它一直在问同样的单词
你认为这是我的误解还是Excel无法生成非常好的随机数?我可以做些什么来让它变得更好?
这是我的代码。
Sub askRandom()
Dim maxRow As Integer
Dim randNum As Integer
Dim answer As Integer
Dim known As Integer
Dim unknown As Integer
Dim sht As Worksheet
Dim resultSheet As Worksheet
Set sht = ThisWorkbook.Sheets("Words")
Set resultSheet = ThisWorkbook.Sheets("Results")
maxRow = sht.Range("A10000").End(3).Row
Do While answer <> vbCancel
randNum = Int(maxRow * Rnd() + 1)
MsgBox sht.Cells(randNum, 1).Value, , "Word is"
answer = MsgBox(sht.Cells(randNum, 2).Value & ", " & sht.Cells(randNum, 3).Value & _
", " & sht.Cells(randNum, 4).Value & sht.Cells(randNum, 5).Value, vbYesNoCancel, _
"Meanings are")
If answer = vbYes Then
known = known + 1
If sht.Cells(randNum, 6).Value <> 0 Then
sht.Cells(randNum, 6).Value = sht.Cells(randNum, 6).Value - 1
End If
ElseIf answer = vbNo Then
unknown = unknown + 1
sht.Cells(randNum, 6).Value = sht.Cells(randNum, 6).Value + 1
End If
Loop
MsgBox "Your results are " & known & " out of " & unknown + known
With resultSheet
.Cells(.Range("A10000").End(3).Row + 1, 1).Value = known
.Cells(.Range("B10000").End(3).Row + 1, 2).Value = unknown
.Cells(.Range("C10000").End(3).Row + 1, 3).Value = Format((known / (known + unknown)) * 100, "%##.0")
.Cells(.Range("D10000").End(3).Row + 1, 4).Value = Now
End With
End Sub
我想,我不能让随机数生成更好,但也许我可以随机播放这些单词&#39;订购。我能这样做吗?我对创新的解决方案持开放态度。
答案 0 :(得分:2)
您可以在致电Randomize
之前尝试使用Rnd()
语句。它为发电机提供了新的种子,可能有助于获得不同的数字。
答案 1 :(得分:1)
正如伊根沃尔夫所说,你需要使用随机化。
VBA的生成器不具有高统计质量,但大部分时间都是诀窍。您可以编码低差异序列并使用随机化&gt;&gt;种子化它们。为了获得质量更好的随机变量。