Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Label1.Text = ""
Dim rnd As New Random
Dim t(50) As Byte
Dim sum(50) As Long
Dim rnd_tmp As Byte
Dim tmp As Byte
For k = 1 To 10000
For n = 1 To 50
t(n) = n
Next
For n = 1 To 7
rnd_tmp = rnd.Next(1, 51)
tmp = t(n)
t(n) = t(rnd_tmp)
t(rnd_tmp) = tmp
Next
For n = 1 To 7
sum(t(n)) += 1
Next
Next
For n = 1 To 50
Label1.Text &= n & " : " & sum(n) & vbNewLine
Next
End Sub
複製代碼
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Label1.Text = ""
Dim rnd As New Random
Dim t(50) As Byte
Dim sum(50) As Long
Dim tmp As Byte
Dim x As Byte
For i = 1 To 10000
x = 0
For n = 1 To 50
t(n) = n
Next
While x < 7
tmp = rnd.Next(1, 51)
If t(tmp) <> 0 Then
t(tmp) = 0
sum(tmp) += 1
x += 1
End If
End While
Next
For n = 1 To 50
Label1.Text &= n & " : " & sum(n) & vbNewLine
Next
End Sub
複製代碼
上面的應該是板主說的方法吧?
會有明顯的機率差
下面的是普通的方法就不會
我想是交換的問題吧
試了一下 改成這樣就沒問題了...
rnd_tmp = rnd.Next(n, 51)
複製代碼
作者: lchihk 時間: 2019-12-22 09:43 PM
這是以前寫的樂透開獎49號隨機出7碼
******************************************
Private Sub Command1_Click()
Dim num(49) As Boolean
For n = 0 To 6
Randomize
FT = True
While FT
Number = Int(49 * Rnd) + 1
If Not num(Number) Then
num(Number) = True
FT = False
End If
Wend
Label1(n).Caption = Number
Next n
End Sub
******************************************