I am trying to generate a random matrix with sum of entries to be equal to 2048. I am generating it by adding 1 to a random element in the matrix given number of times. I am using the following code
X = Table[0, {i, 1, 32}, {j, 1, 32}];
Do[X[[RandomInteger[{1, 32}], RandomInteger[{1, 32}]]]++, {k, 1, 2048}]
However when I sum all entries after a run of a Do cycle I do not obtain 2048. I usually obtain something close to it. What is worse, when I try to increase a value of a single element at the start it often gets erased and it disappears from the matrix. The code I use for this is
X = Table[0, {i, 1, 32}, {j, 1, 32}];
X[[16, 16]]=300;
Do[X[[RandomInteger[{1, 32}], RandomInteger[{1, 32}]]]++, {k, 1, 1748}]
There are some rare instances when 300 remains in the matrix. That corresponds to the case when Do cycle did not try to add 1 into a given element. What am I missing? What is the problem within this code?