Last active
December 20, 2015 21:09
-
-
Save georgepsarakis/6195524 to your computer and use it in GitHub Desktop.
Python random helper functions
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from sys import maxint | |
from random import choice, sample, shuffle | |
from string import ascii_uppercase as U, ascii_lowercase as L, digits as D, punctuation as P | |
def populate(punctuation, extra): | |
chars = U + L + D | |
if isinstance(extra, basestring): | |
chars += extra | |
if punctuation: | |
chars += P | |
return chars | |
''' random string of size N - repeating elements ''' | |
def randomizer(N=60, punctuation=True, extra=''): | |
return "".join([ choice(populate(punctuation, extra) for n in range(N) ] | |
''' random string of size K - unique elements ''' | |
''' (but can be repeatable if you add same elements to extra) ''' | |
def sampler(K=10, punctuation=True, extra=''): | |
return sample(populate(punctuation, extra), K) | |
''' return a shuffled copy of a list ''' | |
def shuffler(L): | |
C = L[:] | |
shuffle(C) | |
return C | |
''' returns a generator providing random integers ''' | |
def ranger(N=10, a=None, b=None): | |
if a is None: | |
a = -(maxint + 1) | |
if b is None: | |
b = maxint | |
return (randint(a,b) for n in xrange(N)) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment