无重复随机数Option ExplicitPrivate Sub Command1_Click()
'-------------------------------------------------------------
' Produces a series of X random numbers without repeating any
'-------------------------------------------------------------
'Results can be used by using array B(X)
Dim A(10000) ' Sets the maximum number to pick
Dim B(10000) ' Will be the list of new numbers (same as DIM above)
Dim Message, Message_Style, Message_Title, Response
'Set the original array
MaxNumber = 10000 ' Must equal the DIM above
For seq = 0 To MaxNumber
A(seq) = seq
Next seq
'Main Loop (mix em all up)
StartTime = Timer
Randomize (Timer)
For MainLoop = MaxNumber To 0 Step -1
ChosenNumber = Int(MainLoop * Rnd)
B(MaxNumber - MainLoop) = A(ChosenNumber)
A(ChosenNumber) = A(MainLoop)
Next MainLoop
EndTime = Timer
TotalTime = EndTime - StartTime
Message = "The sequence of " + Format(MaxNumber, "#,###,###,###") + " numbers has been" + Chr$(10)
Message = Message + "mixed up in a total of " + Format(TotalTime, "##.######") + " seconds!"
Message_Style = vbInformationOnly + vbInformation + vbDefaultButton2
Message_Title = "Sequence Generated"
Response = MsgBox(Message, Message_Style, Message_Title)
'End Sub
'以下为测试部分
'Dim i
'For i = 1 To 50
' Debug.Print B(i)
' Next i
End Sub

解决方案 »

  1.   

    我来格式化它!
    Option ExplicitPrivate Sub Command1_Click()
    '-------------------------------------------------------------
    ' Produces a series of X random numbers without repeating any
    '-------------------------------------------------------------
    'Results can be used by using array B(X)Dim A(10000) ' Sets the maximum number to pick
    Dim B(10000) ' Will be the list of new numbers (same as DIM above)
    Dim Message, Message_Style, Message_Title, Response'Set the original array
    MaxNumber = 10000 ' Must equal the DIM aboveFor seq = 0 To MaxNumber
    A(seq) = seq
    Next seq'Main Loop (mix em all up)
    StartTime = TimerRandomize (Timer)For MainLoop = MaxNumber To 0 Step -1
    ChosenNumber = Int(MainLoop * Rnd)
    B(MaxNumber - MainLoop) = A(ChosenNumber)
    A(ChosenNumber) = A(MainLoop)
    Next MainLoop
    EndTime = Timer
    TotalTime = EndTime - StartTimeMessage = "The sequence of " + Format(MaxNumber, "#,###,###,###") + " numbers has been" + Chr$(10)
    Message = Message + "mixed up in a total of " + Format(TotalTime, "##.######") + " seconds!"
    Message_Style = vbInformationOnly + vbInformation + vbDefaultButton2
    Message_Title = "Sequence Generated"
    Response = MsgBox(Message, Message_Style, Message_Title)'End Sub
    '以下为测试部分
    'Dim i'For i = 1 To 50
    ' Debug.Print B(i)
    ' Next i
      

  2.   

    再来一次!Option ExplicitPrivate Sub Command1_Click()
    '-------------------------------------------------------------
    ' Produces a series of X random numbers without repeating any
    '-------------------------------------------------------------
    'Results can be used by using array B(X)Dim A(10000) ' Sets the maximum number to pick
    Dim B(10000) ' Will be the list of new numbers (same as DIM above)
    Dim Message, Message_Style, Message_Title, Response'Set the original array
    MaxNumber = 10000 ' Must equal the DIM aboveFor seq = 0 To MaxNumber
    A(seq) = seq
    Next seq'Main Loop (mix em all up)
    StartTime = TimerRandomize (Timer)For MainLoop = MaxNumber To 0 Step -1
    ChosenNumber = Int(MainLoop * Rnd)
    B(MaxNumber - MainLoop) = A(ChosenNumber)
    A(ChosenNumber) = A(MainLoop)
    Next MainLoop
    EndTime = Timer
    TotalTime = EndTime - StartTimeMessage = "The sequence of " & Format(MaxNumber, "#,###,###,###") & " numbers has been" & Chr$(10)
    Message = Message & "mixed up in a total of " & Format(TotalTime, "##.######") & " seconds!"
    Message_Style = vbInformationOnly + vbInformation + vbDefaultButton2
    Message_Title = "Sequence Generated"
    Response = MsgBox(Message, Message_Style, Message_Title)'End Sub
    '以下为测试部分
    'Dim i'For i = 1 To 50
    ' Debug.Print B(i)
    ' Next i