{"action":"closed","issue":{"url":"https://api.github.com/repos/rubinius/rubinius/issues/3506","labels_url":"https://api.github.com/repos/rubinius/rubinius/issues/3506/labels{/name}","comments_url":"https://api.github.com/repos/rubinius/rubinius/issues/3506/comments","events_url":"https://api.github.com/repos/rubinius/rubinius/issues/3506/events","html_url":"https://github.com/rubinius/rubinius/issues/3506","id":106428528,"number":3506,"title":"Array#sample sometimes returns duplicate results (when # of sampled items is 1/4 or less of array size)","user":{"login":"alexdowad","id":1067359,"avatar_url":"https://avatars.githubusercontent.com/u/1067359?v=3","gravatar_id":"","url":"https://api.github.com/users/alexdowad","html_url":"https://github.com/alexdowad","followers_url":"https://api.github.com/users/alexdowad/followers","following_url":"https://api.github.com/users/alexdowad/following{/other_user}","gists_url":"https://api.github.com/users/alexdowad/gists{/gist_id}","starred_url":"https://api.github.com/users/alexdowad/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/alexdowad/subscriptions","organizations_url":"https://api.github.com/users/alexdowad/orgs","repos_url":"https://api.github.com/users/alexdowad/repos","events_url":"https://api.github.com/users/alexdowad/events{/privacy}","received_events_url":"https://api.github.com/users/alexdowad/received_events","type":"User","site_admin":false},"labels":[{"url":"https://api.github.com/repos/rubinius/rubinius/labels/kernel","name":"kernel","color":"335c61"}],"state":"closed","locked":false,"assignee":null,"milestone":null,"comments":0,"created_at":"2015-09-14T21:08:23Z","updated_at":"2015-12-28T21:48:22Z","closed_at":"2015-12-28T21:48:22Z","body":"From `ri Array.sample`:\r\n\r\n```\r\nThe elements are chosen by using random and unique indices into the array in\r\norder to ensure that an element doesn't repeat itself unless the array already\r\ncontained duplicate elements.\r\n```\r\n\r\nWhen the number of sampled elements is 1/4 or less of the total array size, `rubinius/kernel/common/array.rb:1378-1390` attempts to use a faster method of selecting the random elements. Unfortunately, this faster code is buggy and can select the same element twice (or even more than twice).\r\n\r\nThe problem is that when it \"spins\" (tries selecting a new random element after finding the one it picked was a duplicate), it doesn't reset `j` to zero. The entire loop starting from line 1378 should be retried, with `j` reset to zero, every time it \"spins\"."}}