code: def countRandomBitFrequencies(numTerms = 100000, percentSparsity = 0.01):
    
    # Accumulate counts by inplace-adding sparse matrices
    counts = SparseMatrix()
    size = 128*128
    counts.resize(1, size)
  
    # Pre-allocate buffer sparse matrix
    sparseBitmap = SparseMatrix()
    sparseBitmap.resize(1, size)
  
    random.seed(42)
  
    # Accumulate counts for each bit for each word
    numWords=0
    for term in xrange(numTerms):
      bitmap = random.sample(xrange(size), int(size*percentSparsity))
      bitmap.sort()
  
      sparseBitmap.setRowFromSparse(0, bitmap, [1]*len(bitmap))
      counts += sparseBitmap
      numWords += 1
  
    # Compute normalized version of counts as a separate matrix
    frequencies = SparseMatrix()
    frequencies.resize(1, size)
    frequencies.copy(counts)
    frequencies.divide(float(numWords))
  
    # Wrap up by printing some statistics and then saving the normalized version
    printFrequencyStatistics(counts, frequencies, numWords, size)
  
    frequencyFilename = "bit_frequencies_random.pkl"
    print "Saving frequency matrix in",frequencyFilename
    with open(frequencyFilename, "wb") as frequencyPickleFile:
      pickle.dump(frequencies, frequencyPickleFile)
  
    return counts
docs: Create a uniformly random counts matrix through sampling.