Skip to content

Instantly share code, notes, and snippets.

@chrisshroba
Last active March 7, 2016 01:32
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 chrisshroba/2f94fc85b8f14d0a137c to your computer and use it in GitHub Desktop.
Save chrisshroba/2f94fc85b8f14d0a137c to your computer and use it in GitHub Desktop.
473 Homework 5 #1a Python Code
import random
def Random(n):
return random.randint(1,n)
def get_k_samples(S, k):
samples = []
for i in range(k):
samples.append(S.pop())
l = k
while len(S)>0:
x = S.pop()
l = l+1
if Random(l) <= k:
samples[Random(k)-1] = x
return set(samples)
###################################################################
###################################################################
####################### Testing it out: ###########################
###################################################################
###################################################################
from pprint import pprint #For pretty printing dictionaries
from collections import Counter #For counting how many times each
# combination occurs in my samples
# Test it out with 100,000 calls
trial_data = [get_k_samples(range(6),3) for it in range(100000)]
trial_data_as_strings = map(str, trial_data)
counted = Counter(trial_data_as_strings)
counted_as_dict = dict(counted)
pprint(counted_as_dict)
@chrisshroba
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment