I am the responsible editor, and stumbled upon that question from HNQ.
My first reaction was to clarify the title because without any context, the title "Why is 1+rand()%6 wrong?" didn't tell me anything about the core issue: "What is wrong? The result?" (and it's a bit click-baity for me personally). Even though it's the actual question, I still couldn't help but to think that the title couldn be improved.
On the other hand, I also struggled to improve the title. I first thought of "Why rand() should be avoided compared to std:rand()", but after reading the answers, it seems the modular arithmetic remainder operation %
is important, and thus I ended with the current title: "Why is rand()%6 considered bias compared to std:rand()?" Anyway, I even acknowledged that the title is still not the best, and I'm open for anyone to improve it.
Regarding the code change, I admit this is a mistake. I intended to fixed a weird 8-space indentation on this revision:
while(x > 6)
x = 1 + std::rand()/((RAND_MAX + 1u)/6); // Note: 1+rand()%6 is wrong!
12345678
and I thought it's already like that from the source. So, when I checked the source, the indentations were normal, but the code has been updated as per Cubbi's comment (revision on cppreference.com). In the end, I replaced it with the latest code on the site, although I also added the superfluous for-loop.
If there's anything that I did wrong, I'll admit it and will take care in the future. On the other hand, I just want any HNQs to have their best presentation.
Aftermath
The title was updated to "Why is rand()%6 biased?", the for-loop has been removed.
However, the OP was confused with the change, the previously accepted answer was deleted. I admit that was the most destructive edit that I've ever done up until now, but I was also really confused why the edit was not rolled back in the first place...
On the other hand, the original question made the article on cppreference.com updated, at least making it clearer than "this code is wrong!". I'm really glad though, because it means we have 2-way feedback (Question about A → Answered on SO → Explanation on A is updated).
But then, will the question be useful to broader audiences in the future in its original revision; other people coming from search engines, not only from SO and HNQ on SE? Will people search "rand()%6 wrong" when it's not stated anywhere now? (Maybe, because Google still indexed the original title in addition to the current title though).
In the end, is the edit a loss or a win; to SO, to SE community, and to broader audiences? I... I really don't know...
for
loop, the problem is not well demonstrated.