1

I would like to fill a 3x3 2D array with values 1,2,3.

I need each number to appear for a given times.

For example:

1 to appear 2 times
2 to appear 4 times
3 to appear 3 times

What I need is to store this numbers to array in a random position.

For Example:

  1,2,2
  3,2,2
  1,3,3

I already did this in a simple way using only 2 different numbers controlled by a counter. So I loop through the 2D array and applying random values of number 1 and number 2. I'm checking if the value is 1 and add it in the counter and the same with number 2. if one of the counter exceeds the number I have set as the maximum appear times then it continues and applies the other value.

Is there any better approach to fill the 3 numbers in random array position?

See code below:

int [][] array = new int [3][3];

int counter1 =0;
int counter2 =0;

for (int i=0; i<3; i++) {
      for (int j=0; j<3; j++) {
        array[i][j] = (int)random(1, 3); //1,2 

          if (arrray[i][j]==1) {
          counter1++;
        } else if (array[i][j] ==2) {
          counter2++;
        } 
       //if it is more than 5 times in the array put only the other value
        if (counter1>5) {
          array[i][j] = 2;
        } 
        //if it is more than 4 times in the array put only the other value
        else if (counter2>4) {
          array[i][j] = 1;
        }
      }
    }

I finally did this according to this discussion:

How can I generate a random number within a range but exclude some?, with 1D array for tesing, but it does not always works. Please see attached code:

int []values = new int[28];
int counter1=0;
int counter2=0;
int counter3=0;

for (int i=0; i<values.length; i++) {
      if (counter1==14) {
        ex = append(ex, 5);
      }  
      if (counter2==4) {
        ex =append(ex, 6);
      }  
      if (counter3==10) {
        ex =append(ex, 7);
      }
      values[i] = getRandomWithExclusion(5, 8, ex);

      if (values[i]==5) {
        counter1++;
      } else if (values[i] ==6) {
        counter2++;
      } else if (values[i] ==7) {
        counter3++;
      }
    }

int getRandomWithExclusion(int start, int end, int []exclude) {
    int rand = 0;
    do {
      rand = (int) random(start, end);
    } 
    while (Arrays.binarySearch (exclude, rand) >= 0);
    return rand;
  }

I would like to fill the 1D array with values of 5,6 or 7. Each one a specific number. Number 5 can be added 14 times. Number 6 can be added 4 times. Number 7 can be added 10 times.

The above code works most of the times, however somethimes it does not. Please let me know if you have any ideas

Community
  • 1
  • 1
Apollon1954
  • 1,388
  • 4
  • 16
  • 33
  • If you have another question, please post it as its own second question instead of editing this question. – Kevin Workman Jul 27 '15 at 15:50
  • Is the same question. I just added what I have done. However this algorithm doesn't work always as expected. @KevinWorkman – Apollon1954 Jul 27 '15 at 16:00
  • Your original question was how to fill a 2D array with particular numbers. You were given several answers that do exactly that. Why don't you use one of those solutions? If you're asking why a specific solution doesn't work, that's a separate question. It's up to you how to proceed, but you won't get many answers if you just keep adding questions to this already answered question. – Kevin Workman Jul 27 '15 at 16:09

2 Answers2

1

This is the Octave/Matlab code for your problem.

n=3;
N=n*n;
count = [1 2; 2 4; 3 3];
if sum(count(:,2)) ~= N
    error('invalid input');
end
m = zeros(n, n);
for i = 1:size(count,1)
    for j = 1:count(i,2)
        r = randi(N);
        while m(r) ~= 0
            r = randi(N);
        end
        m(r) = count(i,1);
    end
end
disp(m);

Please note that when you address a 2D array using only one index, Matlab/Octave would use Column-major order.

Ali
  • 452
  • 5
  • 15
  • This question is about [tag:processing], not matlab. – Kevin Workman Jul 14 '15 at 15:44
  • I think you are mistaken Kevin, as the asker has used the label java! so processing here is meant to be interpretted literally, not as the Processing language. – Ali Jul 15 '15 at 13:14
  • Please research what Processing is. It's a language/library based on Java. View the [other questions](http://stackoverflow.com/search?q=user:495296+[processing]) that the user has posted about Processing. – Kevin Workman Jul 15 '15 at 13:18
1

There are a ton of ways to do this. Since you're using , one way is to create an IntList from all of the numbers you want to add to your array, shuffle it, and then add them to your array. Something like this:

IntList list = new IntList();
for(int i = 1; i <= 3; i++){ //add numbers 1 through 3
   for(int j = 0; j < 3; j++){ add each 3 times
      list.append(i);
   }
}

list.shuffle();

for (int i=0; i<3; i++) {
   for (int j=0; j<3; j++) {
      array[i][j] = list.remove(0);
   }
}

You could also go the other way: create an ArrayList of locations in your array, shuffle them, and then add your ints to those locations.

Kevin Workman
  • 41,537
  • 9
  • 68
  • 107