Skip to content

Instantly share code, notes, and snippets.

@juanfal
Created February 23, 2019 16:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save juanfal/3608e423b2cbf218ace3d10f5d8b062c to your computer and use it in GitHub Desktop.
Save juanfal/3608e423b2cbf218ace3d10f5d8b062c to your computer and use it in GitHub Desktop.
distributing a number of items between a number of cells, randomly. Most efficient way
from numpy import *
def genDist(nitems, ncells):
r = sort(random.randint(0, nitems, ncells, dtype=int))
r[-1] = nitems
r = delete(concatenate((r, [nitems]))-concatenate(([0],r)), -1)
return r
r = genDist(800, 10)
print(r.sum(), r)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment