The sort -R command doesn't sort lines randomly in Linux


Question

I can't get --random-sort to work with the sort command on a Fedora Linux-system.

Some context information:

$ cat /etc/fedora-release
Fedora release 7 (Moonshine)
$ which sort
/bin/sort
$ man sort | grep -A 2 '\-R'
       -R, --random-sort
              sort by random hash of keys

$ man sort | grep -A 3 '\-R'
       -R, --random-sort
              sort by random hash of keys

       --random-source=FILE

And the test:

$ echo -e "2\n1\n3\n5\n4"
2
1
3
5
4
$ echo -e "2\n1\n3\n5\n4" | sort -r # Sort in reverse order
5
4
3
2
1
$ echo -e "2\n1\n3\n5\n4" | sort -R # Sort in random order
1
2
3
4
5
$ # Fail! That's not random (I've tried it multiple times.)
1
11
2/11/2009 3:01:41 PM

It works on my Ubuntu 8.04 machine. Maybe the problem is the source of randoms. From the manual:

--random-source=FILE

         get random bytes from FILE (default /dev/urandom)
# this should give always the same result:
echo -e '2\n1\n3\n5\n4' | sort -R --random-source=/dev/zero

# this should be random:
echo -e '2\n1\n3\n5\n4' | sort -R --random-source=/dev/urandom
16
2/11/2009 3:06:37 PM

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Icon