10

I saw this question on hot yesterday and today it had another title. Why is rand()%6 biased?

At first I thought it is another question similar to the one before. So I looked at it again. The title seemed odd (I would say it is not correct english "biased" would be correct I think) and then I saw that there were also changes to the code. The changes while citing from the source add noise to the question. The loop is not necessary as the topic focuses on the rand() call.

In my opinion the question was worsen with the edit. What am I supposed to do here?

I can not flag the edit or suggest a rollback or something that might be community checked. The only thing that came to mind was flag for moderator attention. Is this correct?

9
  • As the code was taken from the referenced website I don't see that changing it would be correct. If you check the website, editor changed it to the code in the site. en.cppreference.com/w/cpp/numeric/random/rand Though I can see it does seem to stray from OP's intent
    – Suraj Rao
    Commented Apr 18, 2018 at 6:36
  • The edit looks good to me.
    – Maroun
    Commented Apr 18, 2018 at 6:37
  • Should have looked at it today... Will edit this in my question. Thx
    – Kami Kaze
    Commented Apr 18, 2018 at 6:37
  • 1
    It definitely doesnt qualify as vandalism though...
    – Suraj Rao
    Commented Apr 18, 2018 at 6:38
  • @SurajRao Yes surely I assumed the code was not from the reference. Did an edit to mend this.
    – Kami Kaze
    Commented Apr 18, 2018 at 6:40
  • I think that without the for loop, the problem is not well demonstrated.
    – Maroun
    Commented Apr 18, 2018 at 6:44
  • @Maroun I would say that the loop does not demonstrate the problem at all. The bias of the distribution would only be shown with a lot more attempts. But the code executed is not the problem the commented code is the the target of the question and this is not executed at all (so the loop is superflous). Still my question is what to do if I think an edit makes the question worse?
    – Kami Kaze
    Commented Apr 18, 2018 at 6:55
  • 1
    In general, You can ping the editor in the comments below the question and discuss and see if you can rollback...
    – Suraj Rao
    Commented Apr 18, 2018 at 6:59
  • @Maroun Interstingly the content of the reference got updated in regard of the question. (replacing wrong with biased).
    – Kami Kaze
    Commented Apr 18, 2018 at 7:05

1 Answer 1

20

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...

5
  • Please take no offense in this. I saw the change of "wrong" to "biased" in the reference afterwards and I am not sure if it is better to keep the question corresponding to the updated reference or as it was originally. (the question might not have arisen if it would have been this way before....). The question is more about procedure than calling you out. The edit was done with good intentions and that is clearly visible, but I am not sure that it is a general improvement, because the for loop dilutes the code line in question. My suggestion would be at least changing the title bias ->biased
    – Kami Kaze
    Commented Apr 18, 2018 at 9:02
  • Also ( I might be wrong about this as I am not into c++) but is rand() not the same as std::rand()? just one without the namespace but both would be implemented in stdlib.h
    – Kami Kaze
    Commented Apr 18, 2018 at 9:06
  • 1
    @KamiKaze it's okay, I'm not taking offense :) but as a responsible editor, I think I need to make it clear the reasons why I did it. And, yeah, unfortunately I'm not into C++ either. Reading the answers, I thought they seem to differentiate between std::rand() and rand(), so I thought it makes any distinction. However, the title has been updated by other user to "Why is rand()%6 biased?". I'll try to simplify the code too.
    – Andrew T.
    Commented Apr 18, 2018 at 9:12
  • 2
    @AndrewT. The edits on that quesiton are also under discussion on this post. You mgiht consider chiming in there as well, especially since the edits on that post seem to have completely changed what the original question was, and it seems to have caused some flack for the accepted answer.
    – Kendra
    Commented Apr 18, 2018 at 14:20
  • 3
    This has been a very respectful, introspective exchange. Thank you both for posting about this. Commented Apr 19, 2018 at 23:59

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .