'項目:VB中產生在一定正整數范圍內的不重復的隨機數'作者:qq15495835Private Sub Command1_Click() Max = 100 '隨機數上限值 Min = 1 '隨機數下限值 Amount = 10 '產生號碼數量(數量值應小于隨機數上限值-隨機數下限值)否則會產生死循環(huán) ReDim a(Amount) Randomize For i = 0 To Amount a(i) = Int((Max - Min + 1) * Rnd + Min) For j = 0 To i If i j And a(i) = a(j) Then i = i - 1 Next Next Text1 = Join(a, vbCrLf)End Sub'請采納參考!謝謝!
在永善等地區(qū),都構建了全面的區(qū)域性戰(zhàn)略布局,加強發(fā)展的系統(tǒng)性、市場前瞻性、產品創(chuàng)新能力,以專注、極致的服務理念,為客戶提供成都網站建設、網站建設 網站設計制作按需制作,公司網站建設,企業(yè)網站建設,品牌網站建設,網絡營銷推廣,成都外貿網站制作,永善網站建設費用合理。
Dim n As Integer, b As Integer, a(13) As Integer
Private Sub Command1_Click()
If b 5 Then
b = b + 1
x = Int(n * Rnd()) + 1
Text1.Text = Text1.Text a(x) " "
a(x) = a(n)
n = n - 1
Else
Call Form_Load
End If
End Sub
Private Sub Form_Load()
Randomize
n = 13: b = 0
For i = 1 To 13
a(i) = i
Next i
Text1.Text = ""
End Sub
方法名稱:next();功能描述:返回一個0~2147483647之間的整數
方法名稱:next(i);功能描述:返回一個0~i之間的整數
方法名稱:next(i,j);功能描述:返回一個i~j之間的整數
方法名稱:Nextdouble();功能描述:返回一個0~1之間的隨機小數
================
使用方法:
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Dim rand = New System.Random()
MsgBox(rand.next().ToString())
End Sub
==========================
產生N個不同數的話,我給你個我寫的VB的代碼,你稍微改下:
產生0-10的數:
Private Sub Command1_Click()
Randomize
Dim a(10) As Long
n = 10
For i = 0 To n
a(i) = Int(Rnd() * (n + 1))
For j = 0 To i - 1
If a(i) = a(j) Then
i = i - 1
Exit For
End If
Next
Next
For i = 0 To n
Print a(i)
Next
End Sub