Create an almost random number generator in the shell with pwgen, python and expr

I needed a random number generator and had a little bit time for gambling :). I thought about the possibility to create random characters with pwgen and to transform them into the equivalent ascii decimal numbers.

The folowing shell line creates 8 random numbers between 1 and 26:

for i in `pwgen -0 -A 1 8`; do num=`python -c "print ord('$i')"`; echo `expr 123 - $num`; done

The -0 flag for pwgen means no numbers and the -A flag no capitalized characters. The ord method transforms the characters into ascii numbers and expr substracts the ascii number from 123.

The function pwgen creates random characters between a and z. That corresponds to the ascii decimal numbers between 97 and 122.

If you do not have python or pwgen on your system, you can install it with the following line:

sudo apt-get install python pwgen

An example output could be as follows:

~ for i in `pwgen -0 -A 1 8`; do num=`python -c "print ord('$i')"`; echo `expr 123 - $num`; done
24
23
9
10
11
8
14
22

If you need an ascii table to compare the characters with the corresponding decimal numbers, you can use ascii in the command line. Just type the following lines:

sudo apt-get install ascii

ascii

Have fun