Posts Tagged ‘random’

How to randomize the lines of a file?

October 10, 2017 Leave a comment

You have a text file and you want to mix up its lines, i.e. you want to randomize the order of lines.


sort --random-sort input.txt
Categories: bash Tags: ,

A simple command-line media player


You have a folder on your hard drive where you store your MP3s. You would like to play these songs in a random order from the command-line with a simple command.


I have an alarm script with the exact same function; available here. Just modify the configuration part in the header to point to your MP3 folder; then launch it like this:

./ -p

Where -p stands for “play”. The script requires mplayer.

Categories: bash Tags: , ,


There is a useful command in the randomize-lines package namely “rl“. It reads lines from the standard input and shuffles them. It can also be used to pick a random line from a text file.

Let’s see some examples from the man:

Play a random sound after 4 minutes (perfect for toast):

sleep 240 ; play `find /sounds -name ´*.au´ -print | rl --count=1`

Play the 15 most recent .mp3 files in random order.

ls -c *.mp3 | head -n 15 | rl | xargs --delimiter=´\n´ play

Roll a dice:

seq 6 | rl --count 2

Roll a dice 1000 times and see which number comes up more often:

seq 6 | rl --reselect --count 1000 | sort | uniq -c | sort -n

Shuffle the words of a sentence:

echo -n "The rain in Spain stays mainly in the plain." \
| rl --delimiter=´ ´;echo

Find all movies and play them in random order.

find . -name ´*.avi´ -print0 | rl -0 | xargs -n 1 -0 mplayer

Because -0 is used filenames with spaces (even newlines and other unusual characters) in them work.

Categories: bash Tags: , ,

Rock-Paper-Scissors: You vs. the Computer

March 8, 2011 1 comment

Computers mimic human reasoning by building on simple rules and statistical averages. Test your strategy against the computer in this rock-paper-scissors game illustrating basic artificial intelligence. Choose from two different modes: novice, where the computer learns to play from scratch, and veteran, where the computer pits over 200,000 rounds of previous experience against you.” (source)

The novice opponent is not too difficult, but it learns quickly. The veteran opponent is tough though, I had the impression that it can read my mind :) The problem is that we, humans, don’t choose our next move randomly and the computer can analyze our strategy.

Choose randomly

After playing with the veteran for a while, I came up with the idea: “Hey, if I could choose randomly, the computer couldn’t analyze my moves”. So here is a simple random number generator:

C:~> python
Python 2.6.6 (r266:84292, Sep 15 2010, 15:52:39)
[GCC 4.4.5] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import random
>>> li=[1,2,3]
>>> random.choice(li)
>>> random.choice(li)
>>> random.choice(li)

In the “python shell”, you can use the up arrow to call back the previous command(s). With this “cheat” you can surprise the veteran computer :)

I read about this game in this post (in Hungarian).

Update (20110309):

I made a very simple GUI that returns a random number from the interval [1,3] :) Jack’s remark from the comments was taken into account. I’ve never used PyGtk before, I was curious what it’s like. You can get the source code here. Tip: launch the application, right click on it on the task bar, and set “Always On Top” on. This way you can use it together with the game in the browser, you won’t have to switch between the two.

Update (20131007):
There is a Rock Paper Scissors Programming Competition, see the site Here is an example that its author sent to reddit too.

Random post on

December 21, 2010 Leave a comment


On the right side, I’d like to have a list of random posts (with titles, of course). Unfortunately, I don’t find such a widget…

Partial solution

Until I find out how to get a “Random Posts” widget, I have a partial solution. Insert a “Text Widget”, and add the following HTML code to the widget:

<a title="Click here for random post" href="">Click here for random post</a>


Categories: wordpress Tags: ,