' 这样可以吗 ' 生成的随机数范围大一些重复的概率就很小Option ExplicitPrivate Sub Command1_Click() Dim n As Long Randomize Timer n = Int(Rnd * 1000) + 1 Debug.Print n End Sub
' 如果就是刻意要不重复的,看看我刚写的模拟彩票选号的程序。Option Explicit Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long) Dim lblNumber(8) As Label Dim WithEvents cmdStart As CommandButton Dim txtNumber As TextBox Dim pause As Boolean, oldNumber As String Dim i As LongPrivate Sub cmdStart_Click() If cmdStart.Caption = "启动" Then cmdStart.Caption = "停止" Else cmdStart.Caption = "启动" End If Static n As Long Dim j As Long i = CStr(txtNumber.Text) If cmdStart.Caption = "停止" Then Do Until pause = True Sleep 30 Do i = i Mod 30 + 1 Loop Until InStr(oldNumber, "#" & CStr(i) & "#") = 0 txtNumber.Text = CStr(i) DoEvents Loop pause = False Else pause = True n = n + 1 lblNumber(n).Caption = txtNumber.Text oldNumber = oldNumber & txtNumber.Text & "#" Debug.Print oldNumber If n = 8 Then MsgBox "本期中奖号码全部产生!" n = 0 txtNumber.Text = CStr(1) For i = 1 To 8 lblNumber(i).Caption = "*" Next i End If End If End SubPrivate Sub Form_Load() Me.Width = 5000 Me.Height = 5000 oldNumber = "#" For i = 1 To 8 Set lblNumber(i) = Controls.Add("VB.Label", "lblNumber" & CStr(i), Me) With lblNumber(i) .AutoSize = True .Left = 300 * i .Top = 100 .Caption = "*" .Visible = True End With Next iSet txtNumber = Controls.Add("VB.TextBox", "txtNumber", Me) With txtNumber .Text = CStr(1) .Left = 200 .Top = 600 .Width = 1000 .Height = 300 .Visible = True End WithSet cmdStart = Controls.Add("VB.CommandButton", "cmdStart", Me) With cmdStart .Left = 3000 .Top = 600 .Width = 1200 .Height = 500 .Caption = "启动" .Visible = True End With End Sub
个人感觉Rnd()随机概率不太大。 可以自己写一个函数,通过Timer生成Double型随机数,几乎没有重复。(sin cos tan atn exp sqr能产生无理数的都用上去 :) 效率不管)
取数范围小肯定容易重复,另外要正确设置Randomize试试这样 Dim upperbound Dim lowerbound
不好意思各位,忘了把代码贴上来了 Dim rndnum As Integer '定义个随机数字 Randomize rndnum = Int((Form2.List1.ListCount * Rnd + 1) '随机数播放 这样老是重复 谢谢
' 那问题的本质就是产生List1.ListCount个1--List1.ListCount(闭区间)之间不重复的随机整数 ' 在窗体上加一个CommandButton和两个ListBox ' List1 是字符串序列,List2 是将List1 乱序后的序列数。Option Explicit Dim oldNumber As String Dim i As LongPrivate Sub Command1_Click() Dim n As Long List2.Clear For i = 1 To List1.ListCount Do Randomize Timer n = Int(Rnd * List1.ListCount) + 1 Debug.Print oldNumber Loop Until InStr(oldNumber, "#" & CStr(n) & "#") = 0 List2.AddItem CStr(n) oldNumber = oldNumber & CStr(n) & "#" Next i oldNumber = "#" End SubPrivate Sub Form_Load() List1.AddItem "apple" List1.AddItem "orange" List1.AddItem "banana" List1.AddItem "grape" List1.AddItem "fruit" oldNumber = "#" End Sub
' 生成的随机数范围大一些重复的概率就很小Option ExplicitPrivate Sub Command1_Click()
Dim n As Long
Randomize Timer
n = Int(Rnd * 1000) + 1
Debug.Print n
End Sub
Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
Dim lblNumber(8) As Label
Dim WithEvents cmdStart As CommandButton
Dim txtNumber As TextBox
Dim pause As Boolean, oldNumber As String
Dim i As LongPrivate Sub cmdStart_Click()
If cmdStart.Caption = "启动" Then
cmdStart.Caption = "停止"
Else
cmdStart.Caption = "启动"
End If
Static n As Long
Dim j As Long
i = CStr(txtNumber.Text)
If cmdStart.Caption = "停止" Then
Do Until pause = True
Sleep 30
Do
i = i Mod 30 + 1
Loop Until InStr(oldNumber, "#" & CStr(i) & "#") = 0
txtNumber.Text = CStr(i)
DoEvents
Loop
pause = False
Else
pause = True
n = n + 1
lblNumber(n).Caption = txtNumber.Text
oldNumber = oldNumber & txtNumber.Text & "#"
Debug.Print oldNumber
If n = 8 Then
MsgBox "本期中奖号码全部产生!"
n = 0
txtNumber.Text = CStr(1)
For i = 1 To 8
lblNumber(i).Caption = "*"
Next i
End If
End If
End SubPrivate Sub Form_Load()
Me.Width = 5000
Me.Height = 5000
oldNumber = "#"
For i = 1 To 8
Set lblNumber(i) = Controls.Add("VB.Label", "lblNumber" & CStr(i), Me)
With lblNumber(i)
.AutoSize = True
.Left = 300 * i
.Top = 100
.Caption = "*"
.Visible = True
End With
Next iSet txtNumber = Controls.Add("VB.TextBox", "txtNumber", Me)
With txtNumber
.Text = CStr(1)
.Left = 200
.Top = 600
.Width = 1000
.Height = 300
.Visible = True
End WithSet cmdStart = Controls.Add("VB.CommandButton", "cmdStart", Me)
With cmdStart
.Left = 3000
.Top = 600
.Width = 1200
.Height = 500
.Caption = "启动"
.Visible = True
End With
End Sub
可以自己写一个函数,通过Timer生成Double型随机数,几乎没有重复。(sin cos tan atn exp sqr能产生无理数的都用上去 :) 效率不管)
Dim lowerbound
upperbound = 200000
lowerbound = 100000
Randomize (1)
Dim MyValue
MyValue = Int((upperbound - lowerbound + 1) * Rnd + lowerbound)
我是说,比如一个list中有不定量的数据,这样程序每次运行就不会重复。但是如果这个量不变,每次运行都会重复。请问如何解决?
谢谢。
Dim rndnum As Integer '定义个随机数字
Randomize
rndnum = Int((Form2.List1.ListCount * Rnd + 1) '随机数播放
这样老是重复
谢谢
' 在窗体上加一个CommandButton和两个ListBox
' List1 是字符串序列,List2 是将List1 乱序后的序列数。Option Explicit
Dim oldNumber As String
Dim i As LongPrivate Sub Command1_Click()
Dim n As Long
List2.Clear
For i = 1 To List1.ListCount
Do
Randomize Timer
n = Int(Rnd * List1.ListCount) + 1
Debug.Print oldNumber
Loop Until InStr(oldNumber, "#" & CStr(n) & "#") = 0
List2.AddItem CStr(n)
oldNumber = oldNumber & CStr(n) & "#"
Next i
oldNumber = "#"
End SubPrivate Sub Form_Load()
List1.AddItem "apple"
List1.AddItem "orange"
List1.AddItem "banana"
List1.AddItem "grape"
List1.AddItem "fruit"
oldNumber = "#"
End Sub
我想是我的表达能力太差了。我不是说list1.listcount中会循环,我是说,当第一次运行程序时,如果list1.listcount的值是固定的,那么它就会在每次启动后的第一次循环选择时会选择同一个值。
谢谢!谢谢!
楼主想实现什么?