0

so my challenge is to make a MxM zeros matrix but with about 15% of those values as 1s.The 'sum' of all those 1s needs to be as close to 15% as it can, while also making it into a global variable for another function, but for some reason it gets stuck in an infinite loop sometimes. Does anyone know why this is happening?

function [ board ] = randomking1( a,b )
clc
global sum
sum = 0; %initalizes sum to zero

kings = ceil(0.15*a*b); %finds number of kings needed for board size

 board = randi([0,1],a,b); %creates game board of random zeros and ones.
                           % ones being the kings on the board.


 for I = 1:a
        for J = 1:b

             if board(I,J) == 1
                     sum = sum + 1;  %this set of for loops counts the
                                     %number of kings on the board
             end

          end
 end


  while sum > kings || sum < kings-1 %if number of kings on the board is greater than
                       %number needed, the number of ones is reduced. 
       for I = 1:a
         for J = 1:b
               if sum

               if  board(I,J) == 1 %if a board value =1

                      board(I,J) = randi([0,1],1) %randomize 0 or 1

                      if board(I,J) == 0 %if the value becomes zero, subtract from the sum
                   sum = sum - 1
                      end
               end

           end
         end

       end

  disp(sum)
end
3
  • "sometimes"? which loop exactly? for which input parameters does it get stuck? Commented Apr 19, 2015 at 7:58
  • Well sometimes it works and gives me the answer i am looking for, but every once and a while it just infinitely outputs 0 after 0 after 0 vertically. I think the problem might be in the while loop. If i enter randomking1(4,4) it outputs a 3 or a 4. If i enter randomking1(5,5) it outputs a 4 or a 5. But for all of these inputs, randomly it will get stuck in the loop, sometimes the first time i enter it, sometimes the fifth time i run it. Commented Apr 19, 2015 at 8:05
  • It does seem to get stuck more often with the higher number matrices that I enter. Commented Apr 19, 2015 at 8:16

1 Answer 1

2

Instead of trying to find a solution using your bruteforce method, I suggest placing 15 % kings on the board and then randomizing it.

function [ board ] = randomking1( a,b )

board_size = a*b;
% finds number of kings needed for board size
kings = ceil(0.15*board_size); 

% creates game board (as a linear vector) of ones and zeros
% ones being the kings on the board.
board = [ones(1,kings), zeros(1, board_size-kings)];

% shuffle the board
board = board(randperm(board_size));

% create the a x b matrix out of the linear vector
board = reshape(board,[a,b]);
end

Example run: randomking1(3,3):

board_size =

     9


kings =

     2


board =

     1     1     0     0     0     0     0     0     0


board =

     0     1     0     0     0     1     0     0     0


board =

     0     0     0
     1     0     0
     0     1     0
Sign up to request clarification or add additional context in comments.

1 Comment

Sweet! Thanks you so much this really helps with my project!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.