Randomly filled Array

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

    Randomly filled Array

    Hi,

    I need a 2D array with 9 by 9 elements. The problem is that every column must be randomly filled with the numbers 1 To 9 (Or 0 To 8). But every row must contain exactly one 1, one 2 and one 3.

    Can somebody please help me with building this array?

    Cindy
  • kadghar
    Recognized Expert Top Contributor
    • Apr 2007
    • 1302

    #2
    Originally posted by cindy2
    I need a 2D array with 9 by 9 ...
    I see, something like a Sudoku, but without the 3x3 squares.

    I dont know if there is an easy way, but with some DOs and FORs you can achieve it, something like this should work for the first column, you can use something like that for the next ones.
    [CODE=vb]Dim i As Integer
    Dim m As Integer
    Dim IO As Boolean
    Dim Arr(1 To 9, 1 To 9) As Integer
    arr(1,1) = int (rnd * 9) + 1
    i = 2
    Do
    IO = False
    arr(1,i) = int (rnd*9) + 1
    m = 1
    Do
    If arr(1,m) = arr(1,i) Then
    IO = True
    Exit Loop
    End If
    m = m + 1
    If m = i Then Exit Do
    Loop
    If IO = False Then i = i + 1
    If i = 10 Then Exit Do
    Loop[/CODE]

    I think there should be some method for sampling without replacement in wikipedia.

    HTH

    Comment

    • cindy2
      New Member
      • Sep 2007
      • 35

      #3
      Originally posted by kadghar
      ... with some DOs and FORs ...
      I have written a code that produces a 9 by 9 array that contains randomly one "1" in each column and row: So there are never two or more "1" 's in a row or column. My code looks like this:
      [CODE=vbnet] Dim j As Integer
      Dim k As Integer = 8
      Dim i As Integer
      Dim x As Integer

      Dim Arr(8, 8) As Integer
      x = Int(Rnd() * 9)
      Arr(x, 0) = 1

      For i = 1 To 8
      j = Int(Rnd() * 9)
      k = 0
      Do
      Arr(j, i) = 1
      If Arr(j, i) = Arr(j, k) Then
      Arr(j, i) = 0
      j = Int(Rnd() * 9)
      k = 0
      Else
      k = k + 1
      If k = i Then Exit Do
      End If
      Loop
      Next[/CODE]
      The next thing I want to do, is to add the numbers "2" in the same way and of course without overwriting the "1" 's. Can you please help me?

      Cindy

      Comment

      • kadghar
        Recognized Expert Top Contributor
        • Apr 2007
        • 1302

        #4
        Originally posted by cindy2
        The next thing I want to do, is to add the numbers "2" in the same way and of course without overwriting the "1" 's. Can you please help me?
        Hi Cindy, I've made the code that adds all the numbers but there's a small problem here, the algorithm doesn't guarantee that you'll be making a valid arrangement. e.g. while assigning number "4" this might happen:

        0 0 3 1 2 4 0 0 0
        0 4 0 2 0 3 0 1 0
        0 0 4 3 0 1 2 0 0
        0 0 0 4 1 2 3 0 0
        0 3 0 0 4 0 0 2 1
        1 0 0 0 3 0 4 0 2
        2 0 0 0 0 0 1 3 4
        0 1 2 0 0 0 0 4 3
        3 2 1 0 0 0 0 0 0

        So you won't have a chance to write a 4 in the last line. I think the right way should be generating an ordered array like this:

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

        and then randomly switch rows and columns a random number of times.

        HTH

        Comment

        • cindy2
          New Member
          • Sep 2007
          • 35

          #5
          Originally posted by kadghar
          and then randomly switch rows and columns a random number of times.

          HTH
          Thanks for your reply

          I thought of that too, that there may be some inconsisticy while the array is building up.

          In this case only the numbers 1 to 3 have to be at different rows and columns, so I won't encounter inconsistencies . But I want a general solution so that if I want the numbers 1 To 4 at different rows and columns, I only have to make little adjustments to the code.

          I'm pretty new to vb2005, so I dont't know how to swap columns and rows until the requirements fit. Can you please help me?

          Cindy

          Comment

          • cindy2
            New Member
            • Sep 2007
            • 35

            #6
            Originally posted by kadghar
            Hi Cindy, I've made the code that adds all the numbers but there's a small problem here, the algorithm doesn't guarantee that you'll be making a valid arrangement. e.g. while assigning number "4" this might happen:
            Hi kadghar,

            My code produces now a random 9 by 9 array: Both the numbers "1" and "2" are randomized. But even with two random numbers in a 9 by 9 array there will be eventualy an invalid arrangement. So you'r right, The best way is to swap rows and columns until the requierments fit. Can you help me with that please?

            Cindy

            Comment

            • kadghar
              Recognized Expert Top Contributor
              • Apr 2007
              • 1302

              #7
              Originally posted by cindy2
              Hi kadghar,

              My code produces now a random 9 by 9 array: Both the numbers "1" and "2" are randomized. But even with two random numbers in a 9 by 9 array there will be eventualy an invalid arrangement. So you'r right, The best way is to swap rows and columns until the requierments fit. Can you help me with that please?

              Cindy
              Yeap, sorry i didnt answer before, i've been offline for 2 days and now i've a lot of work at the office.

              to generate the initial array do something like

              [CODE=vb]dim arr(8,8) as integer
              dim i as integer
              dim j as integer

              for i=0 to 8
              for j = 0 to 8
              arr(i,j) = ((i + j) mod 9 ) +1
              next
              next[/CODE]

              Depends on de version of vb you are using if you have the mod function. in case you dont, just create it for positive numbers, its quite easy:

              [CODE=vb]public function mod(dou1 as double, dou2 as double) as double
              mod = dou1 - int (dou1/dou2) * dou2
              end function[/CODE]

              and use mod(i+j , 9 ) + 1 instead.

              And then i think you can use randomize to change the seed of the generator and make between 50 and 100 events, do something like:

              [CODE=vb]dim n as integer
              randomize
              n=rnd*50 + 51

              for i = 1 to n
              arr = swap (arr, int(rnd*8), int(rnd*8), int(rnd * 2)+1)
              next[/CODE]

              i dont know if there's an easy way, but you can create a function like

              swap ( swArr, Index1 , Index2, Dimension)

              with a couple of FOR or DO and a dummy one-dimensioned array

              Comment

              • cindy2
                New Member
                • Sep 2007
                • 35

                #8
                Originally posted by kadghar
                And then i think you can use randomize to change the seed of the generator and make between 50 and 100 events, do something like:

                [CODE=vb]dim n as integer
                randomize
                n=rnd*50 + 51

                for i = 1 to n
                arr = swap (arr, int(rnd*8), int(rnd*8), int(rnd * 2)+1)
                next[/CODE]

                i dont know if there's an easy way, but you can create a function like

                swap ( swArr, Index1 , Index2, Dimension)

                with a couple of FOR or DO and a dummy one-dimensioned array
                Thanks for your reply,

                I'm sorry I'm still asking you about the same issue I 'm troubled with. The problem is not to make an ordered array. And even randomize the array is not the real problem. The biggest challenge for me is to make the requirements fit, and when they fit to stop the procedure. I haven't a clue how to mennage that. I also don't understand what you mean with n-times events. Because you still don't know if the requierments will fit in that amount of events...do you? Please, can you help me again? ...Almost forgot: What do you mean with a dummy one-dimensional array

                Cindy

                Comment

                • kadghar
                  Recognized Expert Top Contributor
                  • Apr 2007
                  • 1302

                  #9
                  Originally posted by cindy2
                  ... The biggest challenge for me is to make the requirements fit, and when they fit to stop the procedure. I haven't a clue how to mennage that. I also don't understand what you mean with n-times events...
                  What do you mean with requirements?? If you want the array to have always numbers 1 to 9 without repeating any in columns and rows, all you have to do is with the original array i gave you... the 123456789 , 23456781 ... and swap its rows and cols, it will always preserve this property... nice isnt it?

                  n-times mean that you can swap columns or lines a random number of times, lets say n times, where n will be a random number (lets say betwen 50 and 100), so
                  n= int (rnd * 51 + 50)

                  but you can do it 30 times or the number you like.

                  Originally posted by cindy2
                  Almost forgot: What do you mean with a dummy one-dimensional array
                  Cindy
                  I mean an array that you will dispose after you used it while swaping columns

                  lets say you want to swap column 2 and 4

                  what you should do is copy column 2 into this "dummy array" then copy column 4 into column 2 and then "dummy array" into column 4
                  Last edited by kadghar; Oct 5 '07, 12:08 AM. Reason: typo

                  Comment

                  • cindy2
                    New Member
                    • Sep 2007
                    • 35

                    #10
                    Originally posted by kadghar
                    What do you mean with requirements?? If you want the array to have always numbers 1 to 9 without repeating any in columns and rows, all you have to do is with the original array i gave you... the 123456789 , 23456781 ... and swap its rows and cols, it will always preserve this property... nice isnt it?
                    Thank you very much! My code produces now a random array by swapping rows and colums of an ordered array. It looks very nice, but as you probebly know, the array is not really random. Let's take a look at a much simpler ordered array:

                    1234
                    2341
                    3412
                    4123

                    Indeed, you can swap rows and colums and the requierments (:every row and column can only have each number just ones) will always fit. But with this initial ordered array you can never make the following array:

                    3142
                    4321
                    1234
                    2413

                    So the requierments do fit, but the array can never be made by swapping. My question is: Do you have suggestions to make a really random array? I succeeded in making an array with two random numbers in a x by x array. But it did requiere a lot of code to make just two numbers random.

                    Cindy

                    Comment

                    • kadghar
                      Recognized Expert Top Contributor
                      • Apr 2007
                      • 1302

                      #11
                      Originally posted by cindy2
                      Thank you very much! My code produces now a random array by swapping rows and colums of an ordered array. It looks very nice, but as you probebly know, the array is not really random. Let's take a look at a much simpler ordered array:

                      1234
                      2341
                      3412
                      4123

                      Indeed, you can swap rows and colums and the requierments (:every row and column can only have each number just ones) will always fit. But with this initial ordered array you can never make the following array:

                      3142
                      4321
                      1234
                      2413

                      So the requierments do fit, but the array can never be made by swapping. My question is: Do you have suggestions to make a really random array? I succeeded in making an array with two random numbers in a x by x array. But it did requiere a lot of code to make just two numbers random.

                      Cindy
                      instead of thinking of
                      1234
                      2341
                      3412
                      4123

                      as a base array, think of it as an "index map"

                      so generate a random vector, something that could look like:

                      Arr = {3,2,1,4}

                      and use (imagine indexes go from 1 to 4)
                      arr(1)arr(2)arr (3)arr(4)
                      arr(2)arr(3)arr (4)arr(1)
                      arr(3)arr(4)arr (1)arr(2)
                      arr(4)arr(1)arr (2)arr(3)

                      so the original array will look like:

                      3214
                      2143
                      1432
                      4321

                      And you can swap its columns and rows.
                      This way you can increase the posibilities. even then it'll have some restrictions.

                      HTH

                      By the way, take a look to this thread. It has a similar problem, and the solution there has even more restrictions, if you can find a way to make some better arrays will be great.

                      Comment

                      • cindy2
                        New Member
                        • Sep 2007
                        • 35

                        #12
                        Originally posted by kadghar
                        3214
                        2143
                        1432
                        4321

                        And you can swap its columns and rows.
                        This way you can increase the posibilities. even then it'll have some restrictions.

                        HTH

                        By the way, take a look to this thread. It has a similar problem, and the solution there has even more restrictions, if you can find a way to make some better arrays will be great.
                        That's a good idea, using different index-arrays. I don't want to sound "hard to please", but I want a really random array. I made some examples (on paper), with different index-arrays. It's still impossible to make the array I have written above. So, do you have other suggestions for me?
                        BTW I looked at the thread of Sudoku.

                        Cindy

                        Comment

                        • kadghar
                          Recognized Expert Top Contributor
                          • Apr 2007
                          • 1302

                          #13
                          Originally posted by cindy2
                          ... I don't want to sound "hard to please", ...
                          oh no!! how can you even say it!!! :P just kidding

                          I dont know how many permutations this problem has, but in case you find it out, you'll have to have a "basic structure" for each one, so you could randomly chose one of this structures in the begining, use it as a map of indexes and make the swaps..

                          But at least we have now two basic structures, my 1234 etc... and your imposible one.

                          If you find another case unreachable by any of these two we already have, you can use it as a third one... and so on.

                          Comment

                          • Killer42
                            Recognized Expert Expert
                            • Oct 2006
                            • 8429

                            #14
                            Sorry to drop into the thread so late, but have you tried reading through the Wikipedia entry on magic squares?

                            Comment

                            Working...