The Buridan’s donkey paradox

The Buridan’s donkey is an illustration of a paradox regarding the philosophy of moral determinism and free will.

The paradox shows an hypothetical situation in which a donkey searching for food is caught in the middle of two equally appealing stacks of hay located at the same distance from the donkey. Because the donkey has no real reason to choose one over the other he dies of starvation.

Deliberations_of_CongressI have decided to write a cli program that chooses for me when I can’t make up my mind.

The program is written in C++ and when invoked along with two or more arguments it puts them in a vector and then changes the order randomly.

.. code:: c

#include <iostream>
#include <vector>
#include <algorithm>
#include <random>
#include <string>
#include <sys/poll.h>
using namespace std;

struct pollfd stdin_poll = {
    .fd = fileno (stdin), .events = POLLIN

void read_from_piped_input (vector<string>& lst)
    string x;
    while (getline (cin, x)) {
        lst.push_back (x);

void read_from_arguments (const int& argc, char* argv[], vector<string>& lst)
    if (argc  == 1) {
        cout << "Usage: asino [string] [string] ..." << endl;
    for (vector<string>::size_type i = 1; i < argc; ++i) {
        lst.push_back (argv[i]);

int main (int argc, char* argv[])
    vector<string> lst;
    int poll_ret = poll (&stdin_poll, 1, 0);
    if (poll_ret > 0) {
        read_from_piped_input (lst);
    else {
    read_from_arguments (argc, argv, lst);

    random_device rd;
    mt19937 m(rd());
    shuffle (lst.begin (), lst.end (), m);

    int i = 1;
    for (vector<string>::iterator it = lst.begin (); it != lst.end (); ++it) {
        cout << i++ << ". " << *it << endl;

I have used the Mersenne Twister PRNG just to give it a try.

One of the challenges was to read from stdin instead of arguments when the program is piped after another program in the shell:

.. code:: sh

ls /media/my_movies/ | buridan

So I have used poll() that checks for a specified amount of time if the selected device (/dev/stdin in my case) can perform I/O operations; in my code:

.. code:: bash

poll (&stdin_poll, 1, 0)

I selected the POLLIN as event so poll() only checks if there is data to read, 1 as the number of items in the fds array, 0 milliseconds of timeout because when the program is invoked /dev/stdin may already contain input.

The program should be compiled this way:

.. code:: bash

g++ -std=c++11 ./program.cpp -o output

You are free to reuse this little piece of code as you wish.

EDIT: 02-04-2016 The original idea for the Buridan's donkey came from my mentor Simone Basso who wrote the original one in haskell.

Francesco Mecca