code: def _randSparse(states, actions, mask):
      
      # definition of transition matrix : square stochastic matrix
      P = [None] * actions
      # definition of reward matrix (values between -1 and +1)
      R = [None] * actions
      for action in range(actions):
          # it may be more efficient to implement this by constructing lists
          # of rows, columns and values then creating a coo_matrix, but this
          # works for now
          PP = _sp.dok_matrix((states, states))
          RR = _sp.dok_matrix((states, states))
          for state in range(states):
              if mask is None:
                  m = _np.random.random(states)
                  m[m <= 2/3.0] = 0
                  m[m > 2/3.0] = 1
              elif mask.shape == (actions, states, states):
                  m = mask[action][state]  # mask[action, state, :]
              else:
                  m = mask[state]
              n = int(m.sum())  # m[state, :]
              if n == 0:
                  m[_np.random.randint(0, states)] = 1
                  n = 1
              # find the columns of the vector that have non-zero elements
              nz = m.nonzero()
              if len(nz) == 1:
                  cols = nz[0]
              else:
                  cols = nz[1]
              vals = _np.random.random(n)
              vals = vals / vals.sum()
              reward = 2*_np.random.random(n) - _np.ones(n)
              PP[state, cols] = vals
              RR[state, cols] = reward
          # PP.tocsr() takes the same amount of time as PP.tocoo().tocsr()
          # so constructing PP and RR as coo_matrix in the first place is
          # probably "better"
          P[action] = PP.tocsr()
          R[action] = RR.tocsr()
      return(P, R)
docs: Generate random sparse ``P`` and ``R``. See ``rand`` for details.