Ratonland Menu
Login
|
| Random I am | | Posted by bleader -- Thursday 22nd of January 2009 16:56:58 PM | Named after the Millecolin song, this post is about avoiding to make decisions by yourself and let a random number generator do it for you... So now when you'll be wondering "what shall I eat this evening ?" you can just type "choose pizza kebab guiness^W steack paella chilli" and just go for the one randomly chosen.
The funny part with this is that you'll quickly realize that the "random" choice you make yourself in this kind of situation, are absolutely not random but well chosen based on your feeling, and you'll end up choosing something else than what the script felt on.
It comes in 3 flavors!
Perl:
#!/usr/bin/perl
my $choice = int(rand(scalar(@ARGV)));
print("$ARGV[$choice]\n");
Python (thanks to JP for this one):
#!/usr/bin/python
import random
import sys
print random.choice(sys.argv[1:])
Shell:
#!/bin/sh
choice=$RANDOM
let "choice %= $#"
args=("$@")
echo ${args[$choice]}
| | -- 0 comments -- |
|