用vb怎样生成10个不重复的(1到10)随机数?

2024-12-27 23:06:47
推荐回答(3个)
回答1:

vb生成10个不重复的随机数代码:

Private Sub Command1_Click()

Dim a(9) As Integer

For i = 0 To 9

GoTo way1

End If

Next p

End If

Print a(i) 

Next i

End Sub

扩展资料:

随机数字:

(1)生成随机数比较简单,=rand()即可生成0-RAND_MAX之间的随机数;(#define RAND_MAX 0x7fffu)

(2)如果要是整数,就用=int(rand()%10),表示0至9的整数,以此类推;

(3)如果要生成a与b之间的随机实数,就用=rand()%(b-a+1)+a,就能产生固定位数的整数了,以此类推。

回答2:

Private Sub Command1_Click()
Dim a(9) As Integer
For i = 0 To 9
way1:
Randomize
a(i) = Int(Rnd() * 10) + 1
If i > 1 Then
For p = 0 To i - 1
If a(p) = a(i) Then ‘与前面的对比,如果有重复,重新随机
GoTo way1
End If
Next p
End If
Print a(i) ’打印
Next i
End Sub

回答3:

Private Sub Command1_Click()
Dim a As Integer
a = Rnd(1) * 10
If a > 0 Then
Text1 = a
End If
End Sub