Yahoo奇摩知識+將於 2021 年 5 月 4 日 (美國東部時間) 終止服務。自 2021 年 4 月 20 日 (美國東部時間) 起,Yahoo奇摩知識+服務將會轉為唯讀模式。其他Yahoo奇摩產品與服務或您的Yahoo奇摩帳號都不會受影響。如需關於Yahoo奇摩知識+ 停止服務以及下載您個人資料的資訊,請參閱說明網頁。

Jimmy 發問時間: 電腦與網際網路程式設計 · 2 0 年前

VB~~Rnd~~取亂數不重複

1到50...

用RND取5個亂數

且5個數字不能重複

4 個解答

評分
  • 2 0 年前
    最佳解答

    我用了最簡單的方法,洗牌唷!!

    Private Sub Form_Activate()

    Randomize Timer

    Dim a(0 To 50) As Integer

    For i = 1 To 50

    a(i) = i

    Next i

    For i = 1 To 100

    x = Int(Rnd * 50) + 1: y = Int(Rnd * 50) + 1

    c = a(x): a(x) = a(y): a(y) = c

    Next i

    Print a(1); a(2); a(3); a(4); a(5)

    End Sub

    參考資料: 自己
  • W.J.S.
    Lv 7
    2 0 年前

    表單上放置一個CommandButton

    Private Sub Command1_Click()

    Dim A(1 To 50) As Boolean, I As Integer, N As Integer

    Cls

    Randomize

    For I = 1 To 5

    Do

    N = Int(Rnd * 50) + 1

    Loop Until A(N) = False

    A(N) = True

    Print N;

    Next

    End Sub

  • Sean
    Lv 5
    2 0 年前

    Private Sub Command1_Click()

    Dim a() As Integer

    Dim i As Integer, j As Integer, n As Integer

    Dim rp As Boolean

    Randomize Timer

    Do

    '產生新的數字

    n = Int(Rnd() * 50) + 1

    '檢查是否重覆

    rp = False

    For j = 1 To i

    If a(j) = n Then

    rp = True

    Exit For

    End If

    Next

    '沒有重覆就加入陣列中

    If Not rp Then

    i = i + 1

    ReDim Preserve a(1 To i) As Integer

    a(i) = n

    End If

    Loop Until i = 5

    '顯示 5 個數字

    For i = 1 To UBound(a)

    Debug.Print a(i),

    Next

    Debug.Print

    End Sub

  • ?
    Lv 6
    2 0 年前

    拉一個Button

    Private Sub Command1_Click()

    Dim num%(5), i%, j%, chk As Boolean

    Cls

    For i = 1 To 5

    chk = True

    Do While chk

    Randomize

    num(i) = Int(Rnd * 50) + 1

    chk = False

    For j = 1 To i - 1

    If num(i) = num(j) Then chk = True: Exit For

    Next j

    Loop

    Print num(i);

    Next i

    End Sub

還有問題?馬上發問,尋求解答。