The Buridan’s donkey paradox20 Sep 2015
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.
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.
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:
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:
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:
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.