随机生成n个数

来源:互联网 发布:江西学历网络教育学校 编辑:程序博客网 时间:2024/05/17 09:38

Dim a() As Integer 'n显示随机正整数

Private Sub Command1_Click()

 

n = InputBox("请输入一个正整数nn>=1") 'n用来储存正整数

Call getNinteger(n)

 

End Sub

 

Sub getNinteger(ByVal n As Integer)

 

ReDim a(1 To n) 'p用来显示随即生成的n个数

Dim p As String

Randomize

p = ""

For i = 1 To n

Do

 x = Int(Rnd * 90) + 10 'x是随机生成的正整数

 yes = 0

 For j = 1 To i - 1 '用来判断生成的正整数是否可取

   If x = a(j) Then yes = 1

   Exit For

 Next

Loop While yes = 1

a(i) = x

p = p & Str(a(i)) & ","

 

Next

Label1.Caption = LTrim(Left(p, Len(p) - 1))

 

End Sub

原创粉丝点击