{"action":"opened","issue":{"url":"https://api.github.com/repos/randombit/botan/issues/108","labels_url":"https://api.github.com/repos/randombit/botan/issues/108/labels{/name}","comments_url":"https://api.github.com/repos/randombit/botan/issues/108/comments","events_url":"https://api.github.com/repos/randombit/botan/issues/108/events","html_url":"https://github.com/randombit/botan/issues/108","id":88185013,"number":108,"title":"BigInt::random_integer distribution not uniform","user":{"login":"983","id":11274194,"avatar_url":"https://avatars.githubusercontent.com/u/11274194?v=3","gravatar_id":"","url":"https://api.github.com/users/983","html_url":"https://github.com/983","followers_url":"https://api.github.com/users/983/followers","following_url":"https://api.github.com/users/983/following{/other_user}","gists_url":"https://api.github.com/users/983/gists{/gist_id}","starred_url":"https://api.github.com/users/983/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/983/subscriptions","organizations_url":"https://api.github.com/users/983/orgs","repos_url":"https://api.github.com/users/983/repos","events_url":"https://api.github.com/users/983/events{/privacy}","received_events_url":"https://api.github.com/users/983/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":0,"created_at":"2015-06-14T13:30:39Z","updated_at":"2015-06-14T13:30:39Z","closed_at":null,"body":"Code:\r\nhttps://github.com/randombit/botan/blob/master/src/lib/math/bigint/big_rand.cpp#L45\r\n\r\nTest:\r\n```cpp\r\n#include <vector>\r\n#include <iostream>\r\n#include <botan/bigint.h>\r\n#include <botan/rng.h>\r\n\r\nusing namespace Botan;\r\n\r\nint main(){\r\n\tstd::vector<int> bins(10, 0);\r\n\r\n\tRandomNumberGenerator *rng = RandomNumberGenerator::make_rng();\r\n\r\n\tfor (int i = 0; i < 10000; i++){\r\n\t\tBigInt b = BigInt::random_integer(*rng, 0, bins.size());\r\n\t\tsize_t x = b.to_u32bit();\r\n\t\tbins[x]++;\r\n\t}\r\n\r\n\tfor (size_t i = 0; i < bins.size(); i++){\r\n\t\tstd::cout << i << \": \" << bins[i] << std::endl;\r\n\t}\r\n\r\n\treturn 0;\r\n}\r\n```\r\n\r\nExample output:\r\n```\r\n0: 954\r\n1: 976\r\n2: 1237\r\n3: 1280\r\n4: 945\r\n5: 932\r\n6: 912\r\n7: 897\r\n8: 948\r\n9: 919\r\n```\r\n\r\n2 and 3 appear more often than expected. Note that this happens all the time, not just by chance.\r\n\r\nExplanation:\r\nhttp://stackoverflow.com/questions/10984974/why-do-people-say-there-is-modulo-bias-when-using-a-random-number-generator\r\n\r\nHowever, with this bug I would have expected 0 and 1 to appear more often as well, so maybe there is even more to it."}}