0

This command, which generates numbers from /dev/urandom and prints how many repeated numbers there are, returns the same on every system. Why?

dd if=/dev/urandom count=1 bs=5M 2> /dev/null | od - | cut -d " " -f 2- | sed s/" "/"\n"/g | sort -n | uniq -d | wc -l

This is the command, on every system that I ran this on it returns 256^2, the 16 bit upper limit, the amount of ports on a UNIX system, or just 65536, including on different architectures.

The weird thing about this is, that each number is 6 digits and the data dump, even at 5M, is substantially bigger than the amount of possible numbers ~ It is highly unlikely that the number doesn't get repeated.

Amount of possible numbers: 100000 (because 00000-99999)

Lines in the data dump (5M): 2621441

It prints the same with any dataset above c.a. 1M.

And if I grep in the output for a random number, lets say 045765 it pops up 35-47 times, each time a different amount.

I wrote a small python script to count the numbers, and only one wasn't unique, but that was the last number in the dumb, probably because of the EOF. When printing the data dump it's always longer than the rest of the numbers and just didn't fit, so we can just ignore it. It proves tho that no number is unique, which with 2621441 lines proves that there are more than 65536 unique numbers.

I can't wrap my head around, how this can happen... Some component in that command has to have a bug.

Does anyone have an explanation as to how this can happen?

0

One word: octal.

What od prints is from 000000 to 177777, where digits are 0..7. These strings are just two-byte values encoded differently. The different encoding does not change the fact there are 65536 possibilities. From a large enough random dataset you get them all.

Even if you count "by digit", everything will fit; you just need to do it right, in octal. Your attempt:

Amount of possible numbers: 100000 (because 00000-99999)

assumes decimal. The right method is like this:

  • The first character is 0 or 1, (2 possibilities).
  • Then there are 5 characters 0..7, (8 possibilities for each character).

2 x 85 = 216

1
  • That makes so much sense! Indeed, when I sort it and then only print unique numbers, there are 65537 lines. -1 because of the last number and we got ourselves a winner! – lolsu Apr 8 at 6:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.