java - Weighted random numbers in 2D Array - Processing -


i fill 3x3 2d array values 1,2,3.

i need each number appear given times.

for example:

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

what need store numbers array in random position.

for example:

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

i did in simple way using 2 different numbers controlled counter. loop through 2d array , applying random values of number 1 , number 2. i'm checking if value 1 , add in counter , same number 2. if 1 of counter exceeds number have set maximum appear times continues , applies other value.

is there better approach fill 3 numbers in random array position?

see code below:

int [][] array = new int [3][3];  int counter1 =0; int counter2 =0;  (int i=0; i<3; i++) {       (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 more 5 times in array put other value         if (counter1>5) {           array[i][j] = 2;         }          //if more 4 times in array put other value         else if (counter2>4) {           array[i][j] = 1;         }       }     } 

i did according discussion:

how can generate random number within range exclude some?, 1d array tesing, not works. please see attached code:

int []values = new int[28]; int counter1=0; int counter2=0; int counter3=0;  (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;     {       rand = (int) random(start, end);     }      while (arrays.binarysearch (exclude, rand) >= 0);     return rand;   } 

i fill 1d array values of 5,6 or 7. each 1 specific number. number 5 can added 14 times. number 6 can added 4 times. number 7 can added 10 times.

the above code works of times, somethimes not. please let me know if have ideas

this octave/matlab code 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); = 1:size(count,1)     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 when address 2d array using 1 index, matlab/octave use column-major order.


Comments

Popular posts from this blog

javascript - Using jquery append to add option values into a select element not working -

Android soft keyboard reverts to default keyboard on orientation change -

Rendering JButton to get the JCheckBox behavior in a JTable by using images does not update my table -