Random Array

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • cindy2
    New Member
    • Sep 2007
    • 35

    #1

    Random Array

    The next code produces a random 9 by 9 array. It produces exactly one "1" in each row and column: So there are never two or more "1" 's in a row or column:

    Code:
    Dim j As Integer
            Dim k As Integer
            Dim i As Integer = 0
    
            Dim Rooster(8, 8) As Integer
            j = Int(Rnd() * 9)
            Rooster(j, i) = 1
    
            For i = 1 To 8
                j = Int(Rnd() * 9)
                k = 0
                Rooster(j, i) = 1
                Do
                    Do While Rooster(j, i) = Rooster(j, k)
                        Rooster(j, i) = 0
                        j = Int(Rnd() * 9)
                        Rooster(j, i) = 1
                        k = 0
                    Loop
                    k = k + 1
                    If k = i Then Exit Do
                Loop
            Next
    The next thing I want to do, is adding the numbers 2 in the same way (ofcourse without overwriting the "1" 's). Does somebody know how?

    Cindy
Working...