{"action":"opened","issue":{"url":"https://api.github.com/repos/mrphlip/lrrbot/issues/247","repository_url":"https://api.github.com/repos/mrphlip/lrrbot","labels_url":"https://api.github.com/repos/mrphlip/lrrbot/issues/247/labels{/name}","comments_url":"https://api.github.com/repos/mrphlip/lrrbot/issues/247/comments","events_url":"https://api.github.com/repos/mrphlip/lrrbot/issues/247/events","html_url":"https://github.com/mrphlip/lrrbot/issues/247","id":188461113,"number":247,"title":"pick_random_elements() fails to return results with equal probability","user":{"login":"danieljcrabtree","id":5563424,"avatar_url":"https://avatars.githubusercontent.com/u/5563424?v=3","gravatar_id":"","url":"https://api.github.com/users/danieljcrabtree","html_url":"https://github.com/danieljcrabtree","followers_url":"https://api.github.com/users/danieljcrabtree/followers","following_url":"https://api.github.com/users/danieljcrabtree/following{/other_user}","gists_url":"https://api.github.com/users/danieljcrabtree/gists{/gist_id}","starred_url":"https://api.github.com/users/danieljcrabtree/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/danieljcrabtree/subscriptions","organizations_url":"https://api.github.com/users/danieljcrabtree/orgs","repos_url":"https://api.github.com/users/danieljcrabtree/repos","events_url":"https://api.github.com/users/danieljcrabtree/events{/privacy}","received_events_url":"https://api.github.com/users/danieljcrabtree/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":0,"created_at":"2016-11-10T09:39:59Z","updated_at":"2016-11-10T09:39:59Z","closed_at":null,"body":"I think I've found a small bug in the implementation of algorithm R in `common.utils.pick_random_elements()`. It doesn't return all possible results with equal probability.\r\n\r\nA quick way to test it is to pass in iterable=[0, 1] and k=1. The function will never return [0].\r\n\r\nI've written a small test to check the output.\r\n\r\nhttps://gist.github.com/danieljcrabtree/f1b31cfc20353f3002bc11e441fa8476\r\n\r\n```\r\nITERATIONS = 10000\r\n\r\niterable = [0], k = 1\r\n[0]: 10000\r\nTotal: 10000\r\n\r\niterable = [0, 1], k = 1\r\n[0]: 0\r\n[1]: 10000\r\nTotal: 10000\r\n\r\niterable = [0, 1], k = 2\r\n[0, 1]: 10000\r\nTotal: 10000\r\n\r\niterable = [0, 1, 2], k = 1\r\n[0]: 0\r\n[1]: 4991\r\n[2]: 5009\r\nTotal: 10000\r\n\r\niterable = [0, 1, 2], k = 2\r\n[0, 1]: 0\r\n[0, 2]: 4986\r\n[1, 2]: 5014\r\nTotal: 10000\r\n\r\niterable = [0, 1, 2], k = 3\r\n[0, 1, 2]: 10000\r\nTotal: 10000\r\n\r\niterable = [0, 1, 2, 3], k = 1\r\n[0]: 0\r\n[1]: 3275\r\n[2]: 3369\r\n[3]: 3356\r\nTotal: 10000\r\n\r\niterable = [0, 1, 2, 3], k = 2\r\n[0, 1]: 0\r\n[0, 2]: 1662\r\n[0, 3]: 1669\r\n[1, 2]: 1670\r\n[1, 3]: 1673\r\n[2, 3]: 3326\r\nTotal: 10000\r\n\r\niterable = [0, 1, 2, 3], k = 3\r\n[0, 1, 2]: 0\r\n[0, 1, 3]: 3389\r\n[0, 2, 3]: 3267\r\n[1, 2, 3]: 3344\r\nTotal: 10000\r\n\r\niterable = [0, 1, 2, 3], k = 4\r\n[0, 1, 2, 3]: 10000\r\nTotal: 10000\r\n\r\niterable = [0, 1, 2, 3, 4], k = 1\r\n[0]: 0\r\n[1]: 2601\r\n[2]: 2498\r\n[3]: 2460\r\n[4]: 2441\r\nTotal: 10000\r\n\r\niterable = [0, 1, 2, 3, 4], k = 2\r\n[0, 1]: 0\r\n[0, 2]: 786\r\n[0, 3]: 821\r\n[0, 4]: 857\r\n[1, 2]: 845\r\n[1, 3]: 846\r\n[1, 4]: 804\r\n[2, 3]: 1673\r\n[2, 4]: 1706\r\n[3, 4]: 1662\r\nTotal: 10000\r\n\r\niterable = [0, 1, 2, 3, 4], k = 3\r\n[0, 1, 2]: 0\r\n[0, 1, 3]: 829\r\n[0, 1, 4]: 824\r\n[0, 2, 3]: 795\r\n[0, 2, 4]: 797\r\n[0, 3, 4]: 1720\r\n[1, 2, 3]: 802\r\n[1, 2, 4]: 908\r\n[1, 3, 4]: 1604\r\n[2, 3, 4]: 1721\r\nTotal: 10000\r\n\r\niterable = [0, 1, 2, 3, 4], k = 4\r\n[0, 1, 2, 3]: 0\r\n[0, 1, 2, 4]: 2480\r\n[0, 1, 3, 4]: 2424\r\n[0, 2, 3, 4]: 2534\r\n[1, 2, 3, 4]: 2562\r\nTotal: 10000\r\n\r\niterable = [0, 1, 2, 3, 4], k = 5\r\n[0, 1, 2, 3, 4]: 10000\r\nTotal: 10000\r\n```\r\n\r\nSince `pick_random_elements()` is only called in a few places and always with k=1, perhaps using a built-in like `random.choice()` would be just a effective?"}}
{"actor":{"display_login":"danieljcrabtree"}}