code: def sample_coupling_matrix(dim=3,connectivity=0.5):
      
      max_trial = 10
      check = False
      for trial in range(max_trial):
          # random topology for a given connectivity / edge density
          Coupl = np.zeros((dim,dim))
          n_edges = 0
          for gp in range(dim):
              for g in range(dim):
                  if gp != g:
                      # need to have the factor 0.5, otherwise
                      # connectivity=1 would lead to dim*(dim-1) edges
                      if np.random.rand() < 0.5*connectivity:
                          Coupl[gp,g] = 0.7
                          n_edges += 1
          # obtain adjacancy matrix
          Adj_signed = np.zeros((dim,dim),dtype='int_')
          Adj_signed = np.sign(Coupl)
          Adj = np.abs(Adj_signed)
          # check for cycles and whether there is at least one edge
          if check_nocycles(Adj) and n_edges > 0:
              check = True
              break
      if not check:
          raise ValueError('did not find graph without cycles after',
                           max_trial,'trials')
      return Coupl, Adj, Adj_signed, n_edges
docs: \
      Sample coupling matrix.
  
      Checks that returned graphs contain no self-cycles.
  
      Parameters
      ----------
      dim : int
          dimension of coupling matrix.
      connectivity : float
          fraction of connectivity, fully connected means 1.,
          not-connected means 0, in the case of fully connected, one has
          dim*(dim-1)/2 edges in the graph.
  
      Returns
      -------
      Tuple (Coupl,Adj,Adj_signed) of coupling matrix, adjancancy and
      signed adjacancy matrix.