Recently I have been seeing quite a lot of usage of random.org (to pick out winners of various kinds of competitions; and no I’m not a winner). The documentation on that site are reasonable with regard to pseudo-random number generators but are not quite correct with regard to the source of random numbers under Linux. And for non-cryptographic uses, the following is fine.
The use of random.org momentarily made me wonder how I would do the equivalent at the Unix (or Linux) command-line, and having used the command before, the shuf command came to mind. To be honest shuffling is not what I think of randomisation given how bad I am at shuffling cards, but despite the name, shuf does pretty well at randomising things :-
» seq 1 10 | shuf 4 5 8 7 2 1 10 9 6 3
The seq command generates a sequence from 1-10 as given. It turns out that shuf can do it itself :-
» shuf -i 1-10 7 3 5 6 9 10 8 1 4 2
The most common (relatively) use I have for shuf is to pick out a random line or two from a file. By using the -n option, shuf can do this. The following example makes use of an example file which contains a small number of first names :-
» shuf -n 1 first-names Julian » shuf -n 1 first-names Ian » shuf -n 1 first-names Craig
If you have just a small selection to make, you can provide the list on the command line with the -em option :-
» shuf -n 1 -e Male Female Female
And that is pretty much all there is to it – a simple tool that does just one thing well.