问题如题,这段代码是修改别人的。谢谢了!
Public RowStart As Integer
Public RowEnd As Integer
'定义行数
'Dim Row As Integer
'定义号数
Dim num As Integer
Dim shuru As Integer
Dim NumHis(1 To 500) As Integer
Dim TimerStatus As Integer
Dim TheSame As Integer
Private Sub Image2_Click()
Image2.Picture = LoadPicture("C:\Documents and Settings\Administrator\桌面\selected.gif")
End Sub
Private Sub Form_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single)
If Button = 2 Then ' 检查是否单击了鼠标右键。
PopupMenu caidan ' 把文件菜单显示为一个弹出式菜单。
End If
End Sub
Private Sub Command2_Click()
' X 变量和 Y 变量的尺寸。
Dim xloc, yloc'设置 X 变量和 Y 变量到窗体中心。
xloc = ScaleWidth / 2
yloc = ScaleHeight / 2'显示弹出式菜单。
PopupMenu mnuEdit, vbPopupMenuCenterAlign Or _
vbPopupMenuRightButton, xloc, yloc
End SubPrivate Sub tuichu_Click()
    End
End SubPrivate Sub Command1_Click()
If Command1.Caption = "开始" Then
Timer1.Enabled = True
'Timer2.Enabled = True
Command1.Caption = "停止"
Else
Timer1.Enabled = False
Command1.Caption = "开始"
For i = 1 To 500
  If NumHis(i) = 0 Then
    NumHis(i) = num
    'yiyou(i) = 0
    Exit For
  End If
Next i
Label2.Caption = Label2.Caption & num & vbCrLf
End IfEnd Sub
Private Sub Form_Load()
'ini路径
  Dim k
  IniPath = App.Path & "\Record.ini"
  For i = 1 To Int((Int(Form2.Text1.Text)) + 1)
      k = i & Format("00")
      NumHis(k) = 1
  Exit For
  Next i
'写入
  Title = Format(Date, "dddddd") & " " & Format(Time, "ttttt")
'窗口位置
  Form1.Top = Screen.Height / 2 - Me.ScaleHeight / 2
  Form1.Left = Screen.Width / 2 - Me.ScaleWidth / 2'隐藏按钮
Form1.Command1.Top = Form1.Height - 17000
Form1.Command1.Left = Form1.Width - 21300
'初始Count
  RecCount = 0
End Sub
Private Sub Form_Paint()Me.PaintPicture Me.Picture, 0, 0, ScaleWidth, ScaleHeightEnd Sub
Private Sub Form2_Load()
    shuru = Int(Form2.Text1.Text)
End Sub '初始化变量
Private Sub renshu_Click()
 Form2.Show
End Sub
Private Sub timer1_timer()
    Dim i, j
    For i = 1 To 500
    NumHis(i) = 0
    Next
    j = 0
    Randomize Timer
    While j < 10
        num = Format((Rnd * Int(Form2.Text1.Text)) + 1, "##")
        If NumHis(num) = 0 Then
            Label1.Caption = "" & num & ""
            NumHis(num) = 1
            j = j + 1
        End If
    Wend
    End SubPrivate Sub Timer2_Timer()
If TimerStatus = 0 Then
  For i = 1 To 500
    
      If NumHis(i) = num Then
        
        End If
     
  Next i
End If
End Sub

解决方案 »

  1.   

    Private Sub timer1_timer() 
        Dim i, j 
        For i = 1 To 500 
        NumHis(i) = 0 
        Next 
        j = 0 
        Randomize Timer 
        While j  < 10 
            num = Format((Rnd * Int(Form2.Text1.Text)) + 1, "##") 
            If NumHis(num) = 0 Then 
                Label1.Caption = "" & num & "" 
                NumHis(num) = 1 
                j = j + 1 
            End If 
        Wend 
    End Sub=====================
    Randomize Timer 改成:
    Randomize
      

  2.   

    谢谢 Chen0813大哥的回答
    我按照您的方法去试了,还是会出现重复的结果
    我不知道我这个思路是否正确,还是其他地方出了问题。
      

  3.   

    随机数的种子有问题。
    无论是使用date、timer或GetTickCount函数获得的种子,都可能存在相同的问题,因此,楼主可使用QueryPerformanceCounter函数来产生一个唯一数,然后再根据这个唯一数来产生随机数,这样就不会再有重复的现象。
      

  4.   

    Private Sub timer1_timer()  
        Dim i, j  
        For i = 1 To 500  
        NumHis(i) = 0  
    ...............
        Wend  
    End Sub ===========================================
    你的timer1多长时间触发一次?你的“Private Sub timer1_timer()”中的逻辑,只能保证“单次触发”中的随机数没有重复。你的意思是:假设timer1的timer()事件触发了10次,要这“10次”中的“随机数”没有重复?如果是这样,你要把这一段代码:
      For i = 1 To 500  
          NumHis(i) = 0  
      Next
    移到别处去,比如Form_Load()、“产生随机数”命令按钮的Click()事件等,要看你具体的程序结构来选择。