不同的号码从1到10不同的号码从1到10(Different numbers from 1 to 1

2019-05-12 08:09发布

我想从一个范围内的0-9产生10个不同的号码。 所期望的输出可能看起来像这样,9 0 8 6 5 3 2 4 1 7

Dim arraynum(9) As Integer
Dim crmd As Boolean
Dim rmd as integer

For i = 0 To 9
    arraynum(i) = -1
Next i

crmd = True
Randomize Timer
For i = 0 To 9
    rmd = Int(Rnd * 10)
    For j = 0 To 9
        If arraynum(j) = rmd Then
            j = 9
            If crmd = False Then
                i = i - 1
            End If
            crmd = True
        Else
            crmd = False
        End If
    Next j
    If crmd = False Then
        arraynum(i) = rmd
        QuestionILabel.Caption = QuestionILabel.Caption + Str(arraynum(i))
    End If
Next i

Answer 1:

选择随机值,然后扔掉那些你已经使用是一个主意。 它使较长的运行时间可用号码池变少,因为你把自己的越来越多。

你想要的是我会用下面的代码(因为它的功课伪代码)实现一个洗牌列表:

dim n[10]                 // gives n[0] through n[9]
for each i in 0..9:
    n[i] = i              // initialize them to their indexes
nsize = 10                // starting pool size
do 10 times:
    i = rnd(nsize)        // give a number between 0 and nsize-1
    print n[i]
    nsize = nsize - 1     // these two lines effectively remove the used number
    n[i] = n[nsize]

通过简单地选择从池中随机数,然后用从池上面的数字替换它,减少池的大小,你会得到一个洗牌,而不必担心大量掉了前面的。 这一点很重要,如果数字是,它不引入不必要的启动延迟高。

例如,检查下面的板凳检查:

<--------- n[x] ---------->
for x = 0 1 2 3 4 5 6 7 8 9  nsize  rnd(nsize)  output
---------------------------  -----  ----------  ------
        0 1 2 3 4 5 6 7 8 9     10           4       4
        0 1 2 3 9 5 6 7 8        9           7       7
        0 1 2 3 9 5 6 8          8           2       2
        0 1 8 3 9 5 6            7           6       6
        0 1 8 3 9 5              6           0       0
        5 1 8 3 9                5           2       8
        5 1 9 3                  4           1       1
        5 3 9                    3           0       5
        9 3                      2           1       3
        9                        1           0       9

你可以看到在游泳池降低,当您去,因为你总是有一个未使用的一个替代使用一个,你永远不会有重复。

现在你的功课,包括转向到这一点VB的:-)


而且,由于该作业是现在几乎可以肯定逾期(约一年前),我会后展示如何做到这一点,对于一个完整的解决方案VBA。

Option Explicit
Option Base 0
Sub Macro1()
    Randomize
    Dim list(10) As Integer
    Dim i As Integer
    Dim size As Integer
    Dim pos As Integer
    Dim result As String

    For i = 0 To 9
        list(i) = i
    Next

    size = 10
    result = ":"
    For i = 1 To 10
        pos = Int(Rnd() * size)
        result = result & list(pos) & ":"
        size = size - 1
        list(pos) = list(size)
    Next

    MsgBox result
End Sub

这产生的,在三个不同的运行:

:5:7:4:2:9:1:0:8:3:6:
:3:9:6:0:7:8:5:4:2:1:
:7:6:3:5:1:8:9:0:4:2:


Answer 2:

你需要一个随机排列在0数组9。

我忘了怎么写的基本..类似:

dim a(10)
for i=0 to 9 do a(i) = i
rem do random permute over a:
for i=0 to 9 do 
  j = rand() mod (i+1)
  tmp = a(j)
  a(i) = a(j)
  a(j) = tmp
next i


Answer 3:

Option Explicit 'Force variable declaration

Private Sub Form_Load()
    Dim Ar(1 To 100) As Integer 'The array to store it in
    Dim i, j As Integer 'Counters for loops
    Dim X As Integer 'Variable to store the random generated number
    Dim bFound As Boolean 'Boolean to check if the value has been generated before

    Randomize 'Just once to ensure that we get random values

    For i = 1 To 100
        Do 'Start the loop that generates a random number and checks if it has already been generated
            X = RandomInteger(1, 100) 'Generate a random number
            bFound = False 'Set the boolean to false, if we find the number while searching the array, we'll set it to true which means that we already have that number
            For j = 1 To i 'We only need to check up to i (since we haven't put any values in the rest of the array)
                If Ar(j) = X Then 'If an item of the arrray is the same as the last generated number
                    bFound = True 'Set the boolean to true (it already exists)
                    DoEvents 'To not freeze until the looping is done
                    Exit For 'Since we found it there is no need to check the rest
                End If
            Next
        Loop Until bFound = False 'If it wasn't found then we'll add it, if it was found then we go back to generating a new number and comparing it with all the items of the array
        Ar(i) = X 'Add it to the array
    Next

    ShowInTextBox Text1, Ar 'Just to print the data and see it
End Sub

Private Function RandomInteger(Lowerbound As Integer, Upperbound As Integer) As Integer 'The random number generator code
    RandomInteger = Int((Upperbound - Lowerbound + 1) * Rnd + Lowerbound)
End Function

Private Sub ShowInTextBox(TB As TextBox, A() As Integer) 'Just a sub to show the data in a textbox
    Dim i As Integer

    TB.Text = ""

    For i = 1 To UBound(A)
        TB.Text = TB.Text & CStr(A(i)) & vbCrLf
    Next

    TB.Text = Left$(TB.Text, Len(TB.Text) - 2)
End Sub


Answer 4:

这是最简单不过的工作代码。 也有没有的API!

Dim a(1 To 10), tmp, count, isRepeated
count = 1
Randomize
While count <= 10
    isRepeated = False
    tmp = Left(Rnd * 10, 1)
    For i = 1 To 10
        If tmp = a(i) Then isRepeated = True: Exit For
    Next
    If isRepeated = False Then a(count) = tmp: count = count + 1
Wend
YourLine = Join(a, "")

而这一切,乡亲!



文章来源: Different numbers from 1 to 10
标签: vb6