大家好,我是JNFQOFFICE,本人第一次写博客,不知道如何写,请各位多多指教!
如何产生不相同的随机数
本人只会用VB.net,这里便用VB.NET 来编写!
private function randomff() as integer() '定义一个函数,返回为int型数组
dim a(5) as integer
randomsize()'初始化随机数
for i=0 to 5
a(i)=cint(rnd()*100)+1'产生一个1-100的数(包含1和100)
next
return a
end fubction
private function compares(byval a() as integer) as integer()'比较数组中是否有相同的随机数
for i=0 to a.length-2
for j=i+1 to a.length -1
if a(i)=a(j) then
randomsize()
a(j)=cint(rnd()*100)+1'将a(j)重新生成
a=compares(a) '如果存在就开始重新比较(迭代法)
end if
next
next
return a
end function
public function returnNUM () as string
dim str as string =string.empty
dim a() as integer =randomff()
a=compares(a)
for i=0 to a.length-1
str &=a(i).tostring &space(5)
next
return str
end function
原文链接:https://www.f2er.com/vb/260542.html