The Virtual Candy Dish

 

Good news!  Adding realistic space to the problem more than cuts in half the number of times you’d expect a piece of candy to have been touched. 

 

In the simulation, I represent the container as a square grid, with each grid point holding a piece of candy.  When a piece of candy is removed from the grid, the chance that any piece in the grid is touched falls off exponentially from the site of removal.  That is, the farther away from the site of removal a mint is, the lower the probability that it will be touched. 

 

Figure 1 shows the results for a 16 by 16 grid.  To get these data, 500 trial runs were averaged together. 

 

Figure 1

 

            So, the very last piece of candy picked would’ve been touched about four-and-a-half times.  The 150th piece about one-and-three-quarters time. 

 

            Here is the code I wrote for this virtual communal candy dish, written in the Matlab programming language: 

 

for(numsim =1:500)

            numsim

            touch_results(numsim, :) = mints(16);

end

 

%simulating the number of times a piece of candy in a

%square bin of equally-spaced pieces of candy is touched.

 

function number_times_touched = mints(container_size)

 

grab_location = [ceil(rand*container_size) ceil(rand*container_size)];

mint_there = ones(container_size,container_size);

number_touches = zeros(container_size, container_size);

 

for(number_grabs = 0:container_size^2 - 1)

            %number_grabs

            %Check to see where there are still mints:

            [mint_still_there_row, mint_still_there_column] = find(mint_there);

            %pick one of the remaining mints

           

            grab_index = ceil((rand*length(mint_still_there_row)));

            while(grab_index == 0)

                        grab_index = ceil((rand*length(mint_still_there_row)));

            end

            grab_location = [mint_still_there_row(grab_index),...

                                    mint_still_there_column(grab_index)];

 

            %how many times was this one touched?

            number_times_touched(number_grabs + 1) = number_touches(grab_location(1), grab_location(2));

            %make a note that it's now gone.

            mint_there(grab_location(1), grab_location(2)) = 0;      

 

            %Assume probility of being touched falls off exponentially for the grab sight:

            for(ii = 1:container_size)

                        for(jj = 1: container_size)

                                    if mint_there(ii,jj) == 1

                                                number_touches(ii,jj) = number_touches(ii,jj) +...

                                                            exp(-1*sqrt((grab_location(1) - ii)^2 + (grab_location(2) - jj)^2));

                                    end

                        end

            end

end