# Problems with RandomChoice

Consider an Matrix of dimension n1xn1. I would like to get a list with n2 distinct matrix (of dimension n1xn1), because it involves random numbers. The elements of matrix are numbers 0 or 1. The rules are:

1- The elements of main diagonal of matrix is all 0;

2- The elemnts of first line are 1 with probability z/j and 0 with probability 1 - z/j, where z is a random integer between [1,j-1];

3- The elements of first column are 1 with probability k/i and 0 with probability 1 - k/i, where k is a random integer between [1,i-1];

4- The elements of matrix for i>1, j>1 and i≠j are z/j with prabability 1 and 1-z/j with probability 0, where z is a random integer between [1,j-1].

n1 = 5; (*dimension of matrix*)
n2 = 10; (*number of matrix*)
z[j_] := RandomInteger[{1, j - 1}];(*generate random number integer to first line*)
k[i_] := RandomInteger[{1, i - 1}];(*generate random number integer to first column*)
l[i_, j_] :=
If[j == 1 && i > 1, RandomChoice[{k[i]/i, 1 - k[i]/i} -> {1, 0}],
If[i == 1 && j > 1, RandomChoice[{z[j]/j, 1 - z[j]/j} -> {1, 0}],
RandomChoice[{k[i]/i, 1 - k[i]/i} -> {1, 0}]]];


Generating a matrix using SparseArray

s =  SparseArray[{{i_, i_} -> 0, {i_, j_} -> l[i, j]}, {n1, n1}]


For generate a list with n2 matrix (n1 x n1) I used table:

t= Table[s, {k,10}]


However, the output returns n2 identical matrices. I need different matrices.

So, I decided to use the following command:

s = Table[
SparseArray[{{i_, i_} -> 0, {i_, j_} -> l[i, j]}, {n1, n1}], {n, n2}]


That returns the mistake/error

RandomChoice::weightv: The weights given on the left-hand side of {0,0}->{1,0} should be a list of positive numerical quantities having the same length as the list given on the right-hand side. >>

RandomChoice::weightv: The weights given on the left-hand side of {0,0}->{1,0} should be a list of positive numerical quantities having the same length as the list given on the right-hand side. >>

Can anybody help me?

• @happyfish I don't see where this happens. – SAC Apr 2 '17 at 14:56
• for example, {k, 1-k} can evaluate to {0, 0}. – vapor Apr 2 '17 at 15:07

You get this error because sometimes the input for RandomChoice is {0, 0}. But at the first glance, you wrote code similar to {k[i], 1-k[i]}, which has no obvious mistake. However, you should think carefully how your k is defined. With SetDelayed, the random number generated by k changes each time it is evaluated. So you can get {0, 0} at a probability of $\frac{1}{4}$. You can change your code like this:
l[i_, j_] :=