使用RND和Randomize 函数
Randomize 初始化随机数种子
然后用下面的方式生成一个在Max和Min之间的随机整数
Int((Max- Min+ 1) * Rnd + Min)

解决方案 »

  1.   

    Form_Load 中写:
      Randomize
    必须否则,每次运行产生的随机数顺序都相同。然后,照ACTIVE 说的取界于MAX或MIN 之间的数字,如:
    INT(9-0+1)*RND+0)可以取到0到9之间的随机数
      

  2.   

    一个抽奖程序希望有帮助
    Dim NumBalls% 'declare the need variables
    Dim Winners%(1 To 6)
    Dim Title As StringSub PredictNumbers()Dim inum%, n%, Temp%
    Randomize
    ReDim Balls%(1 To 49)
    Dim LoopCheck%, NoSwap%Timer2.Enabled = False 'disabled the timer
       
    For n = 0 To 5 'reset all labels and back colours
        Label2(n).Caption = ""
        Shape1(n).BackColor = &HC0C0C0
    Next nFor n = 1 To 49: Balls%(n) = n: Next n 'set the 49 balls
    For n = 1 To 6
    LoopCheck = 0Do
      inum = Int(49 * Rnd + 1)
      Loop Until (Balls%(inum) > 0) Or (LoopCheck > 99)
      Balls%(inum) = 0
      Winners%(n) = inum
    Next n
       
    Do
      NoSwap = True
      For n = 1 To 5
      If Winners%(n + 1) < Winners%(n) Then
         Temp = Winners%(n + 1)
         Winners%(n + 1) = Winners%(n)
         Winners%(n) = Temp
         NoSwap = False
      End If
    Next n
    Loop Until NoSwap%Timer2.Enabled = True 'enable the timer
    NumBalls = 1
                                      
    Title = String(30, " ") + "Your chossen lotter numbers for this have been picked      GOOD LUCK!" 'scroll this message
                                   
    End Sub
    Private Sub Cancel_Click()Unload Me 'exit the programEnd SubPrivate Sub Form_Load()Move (Screen.Width - Width) \ 2, (Screen.Height - Height) \ 2 'centre the form on the screenTitle = String(50, " ") + "Lottery Predictor" 'scroll this messageEnd SubPrivate Sub Predict_Click()Call PredictNumbers 'predict the numbersEnd SubPrivate Sub Timer1_Timer()
          
    Title = Mid(Title, 2) & Left(Title, 1)
    Lottery.Caption = Title
                                                                 
    End SubPrivate Sub timer2_Timer()Dim BNum%, BCol&If NumBalls > 6 Then Exit Sub
       BNum = Winners%(NumBalls)
       
       If BNum < 10 Then BCol = &HFFFFFF
       If BNum >= 10 And BNum < 20 Then BCol = &HFF8080
       If BNum >= 20 And BNum < 30 Then BCol = &H8080FF
       If BNum >= 30 And BNum < 40 Then BCol = &HFF00&
       If BNum >= 40 Then BCol = &HFFFF&   Shape1(NumBalls - 1).BackColor = BCol
       Label2(NumBalls - 1).Caption = BNum
       NumBalls = NumBalls + 1End Sub