Private Sub Command1_Click() Randomize For i = 1 To 10 Print Int(10 * Rnd()) Next End Sub
Private Sub Command1_Click() Randomize(now) For i = 1 To 10 Print Int(10 * Rnd()) Next End Sub
Private Sub Riffle(ArrItems() As Integer) Dim i As Long Dim j As Long Dim nLo As Integer Dim nHi As Integer Dim n As Integer nLo = LBound(ArrItems) nHi = UBound(ArrItems) Randomize For i = nLo To nHi j = Rnd * (nHi - nLo) + nLo n = ArrItems(i) ArrItems(i) = ArrItems(j) ArrItems(j) = n Next End SubPrivate Sub PrintItems(ArrItems() As Integer) Dim i As Integer Debug.Print String$(40, "-") For i = LBound(ArrItems) To UBound(ArrItems) Debug.Print ArrItems(i) Next Debug.Print String$(40, "-") End SubPrivate Sub Command1_Click() Dim ArrItems(9) As Integer Dim i As Integer For i = 0 To 9 ArrItems(i) = i Next PrintItems ArrItems() Riffle ArrItems PrintItems ArrItems() End Sub
lihonggen0(李洪根,用.NET,标准答案来了) bobob(bobob) 程序没有10Fearfulness(谁都知道我最拽) 你的程序没有0 楼主要0-10 之间的数啊!Private Sub Form_Click()
Cls Randomize
For i = 1 To 10 Print Int(11 * Rnd()) NextEnd Sub
"产生十个(0-10)内的不同随机整数" 我看上面的大哥们好象没有看仔细。 其实用一个标志就可以了 Private Sub Command1_Click() Static Flag(0 To 10) As String Dim Isok As Boolean Dim Num As Integer Do Isok = True For i = 1 To 10 If Flag(i) <> "Yes" Then Isok = False Exit For End If Next i If Isok = False Then Randomize Num = Int(11 * Rnd()) If Flag(Num) <> "Yes" Then Debug.Print Num Flag(Num) = "Yes" End If Else Exit Do End If Loop End Sub
Private Sub Command1_Click() Randomize For i = 1 To 10 Print Int(10 * Rnd()) Next End Sub
Private Sub Command1_Click() Randomize For i = 1 To 10 Print Int(11 * Rnd()) Next End Sub这样不就可以了吗? 还用其它的方法吗?
怎么个叫最好?
Randomize
Print Int(10 * Rnd() + 1)
每次得到随机前要randomize一下,初始化!
Randomize
For i = 1 To 10
Print Int(10 * Rnd())
Next
End Sub
Randomize(now)
For i = 1 To 10
Print Int(10 * Rnd())
Next
End Sub
Private Sub Riffle(ArrItems() As Integer)
Dim i As Long
Dim j As Long
Dim nLo As Integer
Dim nHi As Integer
Dim n As Integer
nLo = LBound(ArrItems)
nHi = UBound(ArrItems)
Randomize
For i = nLo To nHi
j = Rnd * (nHi - nLo) + nLo
n = ArrItems(i)
ArrItems(i) = ArrItems(j)
ArrItems(j) = n
Next
End SubPrivate Sub PrintItems(ArrItems() As Integer)
Dim i As Integer
Debug.Print String$(40, "-")
For i = LBound(ArrItems) To UBound(ArrItems)
Debug.Print ArrItems(i)
Next
Debug.Print String$(40, "-")
End SubPrivate Sub Command1_Click()
Dim ArrItems(9) As Integer
Dim i As Integer
For i = 0 To 9
ArrItems(i) = i
Next
PrintItems ArrItems()
Riffle ArrItems
PrintItems ArrItems()
End Sub
Cls
Randomize
For i = 1 To 10
Print Int(11 * Rnd())
NextEnd Sub
我看上面的大哥们好象没有看仔细。
其实用一个标志就可以了
Private Sub Command1_Click()
Static Flag(0 To 10) As String
Dim Isok As Boolean
Dim Num As Integer
Do
Isok = True
For i = 1 To 10
If Flag(i) <> "Yes" Then
Isok = False
Exit For
End If
Next i
If Isok = False Then
Randomize
Num = Int(11 * Rnd())
If Flag(Num) <> "Yes" Then
Debug.Print Num
Flag(Num) = "Yes"
End If
Else
Exit Do
End If
Loop
End Sub
Randomize
For i = 1 To 10
Print Int(10 * Rnd())
Next
End Sub
Randomize
For i = 1 To 10
Print Int(11 * Rnd())
Next
End Sub这样不就可以了吗? 还用其它的方法吗?
liu584() Intelement(智能元素)两位仁兄的能够产生不重复的数
For i = 0 To 10
Flag(i) = "no"
Next
不然,执行一次之后,就会因Flag(i)全部被置为"yes",而出现问题。