Mindf**k of the day — rand() from c++ and Berlekamp–Massey algorithm

Revision en3, by Radewoosh, 2019-02-26 22:59:48

Once, during one training, my team and I noticed something strange. I'll write a little bit later what it was exactly. The point is that after the contest we decided to investigate how c++ rand() behaves on our machines (we use Linux). So, we used a Berlekamp–Massey algorithm on the results of rand() taken modulo 2.

The result is shocking — the length of the linear recurrence is only 527! No, the results aren't cyclic, just the recurrence is short.

"Cool, but who cares? Just some random fact which I wouldn't notice and it wouldn't have any impact on me." — wrong!

We discovered it while solving a task which required calculating the rank of a binary matrix. We wanted to check something, so we just generated big matrix using rand(), which turned out to be a bad decision. As only the first 527 columns were, let's say, independent, the rest were determined by first 527 ones, so the rank didn't exceed 527, and we lost much time just wondering what's happening.

Moreover, if the number of columns is even, the rank is only 496 (for matrices big enough of course). Guess what's the length of the recurrence if we look only at every second value of rand().

Yep, you're right — 496. One can notice that 527 - 496 = 31 and that 31 divides 527! Illuminati!

Here are some useful links: 1 2.

If anybody can explain precisely what's happening for even number of columns, it would be great. Also, if anybody knows how does rand() look inside and can say a few words about it, it also would be great.

Tags rand(), c++, berlekamp-massey


  Rev. Lang. By When Δ Comment
en3 English Radewoosh 2019-02-26 22:59:48 9 Tiny change: 'i!\n\nHere's a useful li' -> 'i!\n\nHere are some useful li'
en2 English Radewoosh 2019-02-26 22:58:30 47 Tiny change: 'deone.com/NjVg0F) [2](http' -> 'deone.com/REytPv) [2](http'
en1 English Radewoosh 2019-02-26 22:47:06 1646 Initial revision (published)