Last active
December 30, 2021 17:56
-
-
Save conqp/89aad1a69e729f0e3b6b95828b42d8e3 to your computer and use it in GitHub Desktop.
check random subset size distributions
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 collections import defaultdict | |
from functools import partial | |
from json import dumps | |
from random import * | |
l = [5, 1, 2, 7, 4] | |
f = lambda x: [i for _, i in sample(list(enumerate(x)),k=randrange(len(x))+1)] | |
def g(a): | |
n=len(a);s=randint(1,2**n-1);t=[j for j in range(n)if s&2**j] | |
return [a[i] for i in t] | |
def test(func): | |
probs = defaultdict(int) | |
for _ in range(1_000_000): | |
s = func(l) | |
probs[frozenset(s)] += 1 | |
return probs | |
def main(): | |
results = {} | |
for func in [f, g]: | |
results[func.__name__] = {str(key): value for key, value in test(func).items()} | |
print(dumps(results, indent=2)) | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment