1 Module RandNumber 2 Public Nums() As Integer 3 Public Sub StartCreateNumber(TopNumber As Integer) 4 Dim i, j, k As Integer 5 Dim temp(TopNumber) As Integer 6 ReDim Nums(TopNumber) 7 Dim rnd As New Random(Seed:=Date.Now.Second) 8 For i = 1 To TopNumber 9 Nums(i) = 0 10 Next 11 For j = 1 To TopNumber 12 k = rnd.Next(1, TopNumber + 1) 13 While Nums(k) <> 0 14 k = rnd.Next(1, TopNumber + 1) 15 End While 16 Nums(k) = j 17 Next 18 End Sub 19 End Module
本例子是从 1至 n(代码里编号名为TopNumber)生成随机的不重复的数字。
主要思想是 随机生成数据下标,对生成的下标 进行顺序从1至n 赋值。
代码是用模块写的,可以随意调用。