Skip to content

Instantly share code, notes, and snippets.

@aji
Created January 3, 2020 06:34
Show Gist options
  • Save aji/1067547411ec4e7690da0fd6bf0b5e13 to your computer and use it in GitHub Desktop.
Save aji/1067547411ec4e7690da0fd6bf0b5e13 to your computer and use it in GitHub Desktop.
import numpy as np, sys, random, argparse
parser = argparse.ArgumentParser()
parser.add_argument('-n', type=int, default=10)
parser.add_argument('-s', type=float, default=1.0)
args = parser.parse_args()
words = [x.strip() for x in sys.stdin]
partials = np.power(1.0 + np.arange(len(words)), -args.s).cumsum()
pick = lambda: words[partials.searchsorted(random.random() * partials[-1])]
print('\n'.join(pick() for i in range(args.n)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment