469,090 Members | 1,217 Online

# Random Array

35
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:

Expand|Select|Wrap|Line Numbers
1. Dim j As Integer
2.         Dim k As Integer
3.         Dim i As Integer = 0
4.
5.         Dim Rooster(8, 8) As Integer
6.         j = Int(Rnd() * 9)
7.         Rooster(j, i) = 1
8.
9.         For i = 1 To 8
10.             j = Int(Rnd() * 9)
11.             k = 0
12.             Rooster(j, i) = 1
13.             Do
14.                 Do While Rooster(j, i) = Rooster(j, k)
15.                     Rooster(j, i) = 0
16.                     j = Int(Rnd() * 9)
17.                     Rooster(j, i) = 1
18.                     k = 0
19.                 Loop
20.                 k = k + 1
21.                 If k = i Then Exit Do
22.             Loop
23.         Next
24.
25.
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
Sep 27 '07 #1
0 830