I am a software developer and recently came across this problem when working on a hobby project, and my knowledge of probability is too small to solve this.
I have a bag that contains 5 negative ones, 5 zeros, 10 ones, 5 twos, 5 threes. The tiles are set randomly on a 5 by 5 grid with 5 tiles left in the bag. The threes in the bag also multiply all 8 of the adjacent squares by 2 (A number is multiplied by $2^n$ adjacent 3's). What is the best way to calculate the expected value of the grid for any known bag that can contain a random amount of any numbers? Bonus points if it can handle any size (and rectangular shape) of grid.
Eg. Bag ( 2 1 3 3 3 ), Grid Value: 27
$$\begin{array}{c|c|c|c|c} 3&2&1&-1&1\\ \hline 1&1&0&0&-1\\ \hline 1&0&3&2&2\\ \hline -1&0&-1&2&1\\ \hline 1&-1&1&1&0 \end{array}$$
Without the multiplier, the solution is quite simple:
The number of values in the grid (25) × The sum of all of the numbers in the bag / the number of numbers in the bag. Or $(n * s) / a$
Since they do not interact, the fact they are in a grid does not even matter. For the same reason, it's not that useful for solving this problem, but I thought I would mention it as a start.
The simplest approach--in the sense of requiring the least mathematics--is via simulation. You would program random permutations and compute the value of the grid for each one; with sufficiently large samples, the expectation would be reasonably close to the exact value, and you can also get an estimate of the standard deviation of the value.
My code in Mathematica:
This produces $10^6$ simulations and computes the overall mean and variance. Note the following:
An exact calculation would be problematic, since the number of such grids is quite large, even after accounting for symmetries and repeated elements.
The conditional means and variances for each base total ranging from $25$ to $40$ are approximately:
$$\begin{array}{c|cc} \text{base} & \text{mean} & \text{variance} \\ \hline 25 & 25. & 0. \\ 26 & 31.4203 & 4.60442 \\ 27 & 36.0449 & 18.0836 \\ 28 & 40.4705 & 19.7906 \\ 29 & 44.906 & 31.9896 \\ 30 & 49.4376 & 36.9694 \\ 31 & 53.8249 & 47.2579 \\ 32 & 58.3649 & 55.2471 \\ 33 & 62.7921 & 61.3061 \\ 34 & 67.0464 & 69.8355 \\ 35 & 71.4379 & 73.5359 \\ 36 & 75.1243 & 69.9286 \\ 37 & 77.7472 & 68.0978 \\ 38 & 79.9451 & 67.8064 \\ 39 & 82.2345 & 66.5388 \\ 40 & 85.338 & 70.5698 \\ \end{array}$$ This is based on $10^7$ simulations.