3

This is the quote from Linux sort utility manual:

-R, --random-sort sort by random hash of keys

Isn't this a form of oxymoron? How can you sort by random, if usually random lists are not in order?

In other words, is that a proper wording?


Searching in Google "random sort" gives >190k results, so it seems it's quite popular expression to say that something is sorted by random ("sorted by random" gives also >187k results).

7
  • 1
    Have you forgotten that nothing in computers is actually "random"?
    – Catija
    Commented May 19, 2015 at 15:48
  • 9
    Is it just me, or are you not quite parsing this correctly? I don't think you can just ignore the word hash here. You can most certainly assign a random number to each record in a table, and then you can sort the table by those randomly-generated numbers. That's sorting by a set of random numbers, which isn't necessarily the same as "sorting by random." P.S. @Catija - The numbers you get from here are random.
    – J.R.
    Commented May 19, 2015 at 16:20
  • 2
    As @J.R. notes, you aren't sorting by random, you're sorting by [a type of] hash. Random just happens to be the term that computer engineers have assigned the concept; whether or not the hash that is generated is truly mathematically random is irrelevant. If they called computer randomness "purpleness" then you'd be sorting by purple hash, not by the concept of purpleness itself. The more interesting question is how to parse sort randomly (compare sort alphabetically, sort chronologically), and whether sort randomly is oxymoronic.
    – choster
    Commented May 19, 2015 at 16:40
  • 1
    @Catija, never say never -- I know /dev/random pushes the limits of randomness obtained from deterministic input, but there are devices based on atomic decay, diode noise, even a lava lamp IIRC.
    – Chris H
    Commented May 19, 2015 at 19:06
  • 1
    It doesn't say "sort by random", it says "sort by random hash". To a computer programmer it makes sense and means that the keys are "hashed" (converted into numbers) using a hash algorithm intended to produce a random, unpredictable value from each key. Then the sort is performed on those values. This effectively randomizes the entire list.
    – Hot Licks
    Commented May 19, 2015 at 22:32

6 Answers 6

6

In my head

sorting

Is really just ordering. There's no inherent requirement that the ordering in particular be well-defined, ascending/descending, or aesthetically pleasing.

M-W definition 3

the act of separating things and putting them in a particular order : the act of sorting things

A random order doesn't seem to break these definitions.


Let me expand my answer taking in the comments below and getting vaguely rigorous. No need to continue if you've been convinced.

A collection may be stated as

Collection = { x1, x2, ... , xn }

An order is a function that goes from N -> N where N is a natural number 1..n.

For a collection to be ordered that just means I can generate a list

(x1, 1)

(x2, 2)

(x3, 3)

Where the left hand side is the item and the right hand side is the position.

An order function just transforms positions. A random ordering might be

f:

1 -> 3

2 -> 1

3 -> 2

I would apply that order function to get a new ordering out. I have successfully separated things and put them in a particular order. The new collection is still ordered, it's just been sorted into a different one.


Vaguely philosophical addendum

The idea of sorting has to be this vague because you aren't limited to sorting items lexicographically or numerically. Let's say I want to sort a set of countries. The countries themselves cannot be ordered meaningfully but I can define an ordering based on items that I know how to order. I can do it by population, or by area, or any number of other metrics. I also might be able to do it by ratio of people to cattle or any number of arbitrary functions. Because the potential orderings are so arbitrary all we can really say about them is that they are orderings. You'll have a bugger of a time trying to define rules for ordering that capture all potential valid cases. After all, it's perfectly valid for me to say that I'm going to sort this list of countries based on how happy they make me. These countries are now being judged on something that's outside the system where the system is the list. In a similar way, your random ordering is being sorted by something that's outside the system where the system is the list. In the case of the random sort, it's likely truly being ordered on a function of the system time.

5
  • 'a particular order' seems to refer to the specific fixed order, however random is always in not a particular order, so I'm still not quite clear if it doesn't break this definition.
    – kenorb
    Commented May 19, 2015 at 15:51
  • 1
    An order is a mapping from an element to an index or position. How this order was arrived at is orthogonal to the discussion. Let me rephrase sorting as the act of applying that order to the collection of items.
    – Paarth
    Commented May 19, 2015 at 15:55
  • 1
    “In a particular order” doesn’t necessarily mean a specific, fixed order. You can put something into any order you want, and you’re still ‘sorting’ the things. If you sort things by something that is random, you’ll end up with a list of things in random order—that doesn’t mean they’re not sorted. Commented May 19, 2015 at 15:56
  • @kenorb added more detail to my answer in response to your comment.
    – Paarth
    Commented May 19, 2015 at 16:09
  • sort uses /dev/urandom to seed its random number generator. /dev/urandom gets its randomness from the environment; physical processes that are unpredictable (and quite a bit of effort goes into ensuring they are unpredictable). That means the seed is unpredictable, and unless you're using a debugger on sort to obtain the seed, it's unreproducible. I'd say it's more appropriate to call this shuffling rather than sorting. But programmers often name things poorly.
    – jpkotta
    Commented May 19, 2015 at 19:56
2

What the entry in the manual says is:

-R, --random-sort sort by random hash of keys

Ie, the software hashes each entry using a hashing function primed with a "random" state and then, using a normal deterministic sorting algorithm, sorts the entries based on their hashes. (Random is a property of the hash.)

This is arguably an overly implementation-oriented description of this option (it may be a better idea to describe the end result instead of the implementation in the user documentation) but the quote from the documentation is not an oxymoron.


Then we have your question, whether sort by random is an oxymoron, even though this ends up not actually being applicable to the quoted text.

This use (or variations thereof, I think sort randomly would sound better), is something that does occur to some extent. However, my impression is that this can be attributed to computer jargon primarily, where sort doesn't necessarily have the same implication as it does normally.

Looking at dictionary definitions of sort will essentially tell you that it's arranging items based on some quality. Considering that the definition does have an implication of bringing order, this use does seem to contradict that.

I think that order randomly would be a more generally accepted alternative. (Dictionaries include a meaning of simply arranging items in a certain order, for one thing.)

2
  • I realised that it doesn't apply exactly to the quoted text, however the option is still called "random-sort" (unless it's just a shortcut). Searching in Google "random sort" gives >190k results, so it seems it's quite popular to say that something is sorted by random ("sorted by random" - >187k results).
    – kenorb
    Commented May 19, 2015 at 19:47
  • @kenorb I did attempt to address how this is used as well, does my take on this not fit with how you've seen it used? I'm not sure how much one should read into the option --random-sort but you could argue that in itself is an oxymoron, even though the descriptive text is not. Commented May 19, 2015 at 20:04
2

"Random" does not apply to "sort" in this example, but to "hash of keys".

You shouldn't read it as "sort by random, hash of keys", but as "sort by the keys, which themselves are random", so you are not "sorting by random", but sorting by the keys / hashes, which might happen to be random, but the sorting is done with regard to these keys (or hashes), and is not random by itself.

3
  • This answer sounds almost correct. But you seem to be implying that random applies to keys. That is not the case. Random applies to hash. The keys are not random at all.
    – kasperd
    Commented May 19, 2015 at 21:43
  • @kasperd : you are right. I didn't accentuate the hash because key is easier to understand for the non-technical readers and in this case the hashes are used as a type of key anyway.
    – vsz
    Commented May 20, 2015 at 6:05
  • The wording "keys, which themselves are random" is still not correct because the keys are not random. The wording "hash of keys" makes it clear that "keys" doesn't mean the output of the hash but rather (part of) the input to the hash.
    – kasperd
    Commented May 20, 2015 at 7:22
1

Technically speaking, no. It is an oxymoron. Shuffle is more fitting than random.

1
  • The process being describe is indeed shuffling, but it's accomplished, as @vsz says, through sorting by a (random hash of keys).
    – Chris H
    Commented May 19, 2015 at 19:04
0

Nothing in computing is technically "random"... Only a programmed algorithm that it follows to make something seem random:

You can program a machine to generate what can be called “random” numbers, but the machine is always at the mercy of its programming. “On a completely deterministic machine you can’t generate anything you could really call a random sequence of numbers,” says Ward, “because the machine is following the same algorithm to generate them. Typically, that means it starts with a common ‘seed’ number and then follows a pattern.” The results may be sufficiently complex to make the pattern difficult to identify, but because it is ruled by a carefully defined and consistently repeated algorithm, the numbers it produces are not truly random. “They are what we call ‘pseudo-random’ numbers,” Ward says.

So, when you tell a program to sort by "random" that tells the computer to sort using the algorithm called "random".

3
  • 1
    Ward has neatly summarized PNRGs, but that's not the end of the story on that topic.
    – J.R.
    Commented May 19, 2015 at 16:36
  • 1
    There are things in computing that are technically random. There are hardware modules which generate random bits based on random physical phenomena. Some aspects of the timing of physical events can be random. Commented May 19, 2015 at 17:16
  • 1
    You quoted the second paragraph of that article. Now quote the fourth. Commented May 19, 2015 at 19:24
0

The word random in that context applies to the word hash. Instead of "sort by random hash of keys" imagine it had said "sort by a hash of keys".

The later version doesn't say how that hash was chosen, and it doesn't use the word random at all.

In reality what happens is that the program has 340 sixtillion hashes to choose from, and it chose one at random. So what those words intend to communicate is that the program will first chose one hash at random and then it will sort all of the keys according to the one chosen hash.

You must log in to answer this question.

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