code: def random_weights(n, bounds=(0., 1.), total=1.0):
      
      low = bounds[0]
      high = bounds[1]
  
      if high < low:
          raise ValueError('Higher bound must be greater or '
                           'equal to lower bound')
  
      if n * high < total or n * low > total:
          raise ValueError('solution not possible with given n and bounds')
  
      w = [0] * n
      tgt = -float(total)
  
      for i in range(n):
          rn = n - i - 1
          rhigh = rn * high
          rlow = rn * low
  
          lowb = max(-rhigh - tgt, low)
          highb = min(-rlow - tgt, high)
  
          rw = random.uniform(lowb, highb)
          w[i] = rw
  
          tgt += rw
  
      random.shuffle(w)
      return w
docs: Generate pseudo-random weights.
  
      Returns a list of random weights that is of length
      n, where each weight is in the range bounds, and
      where the weights sum up to total.
  
      Useful for creating random portfolios when benchmarking.
  
      Args:
          * n (int): number of random weights
          * bounds ((low, high)): bounds for each weight
          * total (float): total sum of the weights