code: def noisy(pattern, noiseLevel, totalNumCells):
    
    n = int(noiseLevel * len(pattern))
  
    noised = set(pattern)
  
    noised.difference_update(random.sample(noised, n))
  
    for _ in xrange(n):
      while True:
        v = random.randint(0, totalNumCells - 1)
        if v not in pattern and v not in noised:
          noised.add(v)
          break
  
    return noised
docs: Generate a noisy copy of a pattern.
  
    Given number of active bits w = len(pattern),
    deactivate noiseLevel*w cells, and activate noiseLevel*w other cells.
  
    @param pattern (set)
    A set of active indices
  
    @param noiseLevel (float)
    The percentage of the bits to shuffle
  
    @param totalNumCells (int)
    The number of cells in the SDR, active and inactive
  
    @return (set)
    A noisy set of active indices