Posted: . At: 12:06 PM. This was 6 years ago. Post ID: 12251
Page permalink. WordPress uses cookies, or tiny pieces of information stored on your computer, to verify who you are. There are cookies for logged in users and for commenters.
These cookies expire two weeks after they are set.


how to generate long random numbers with the Linux command line.


This simple command prints out a long random number string from /dev/urandom.

jason@Yog-Sothoth » img » $ tr -dc "[:digit:]" < /dev/urandom | head -c 64 ; echo
9958048462925775253231939134565711861924198983498274782350446110

Do it this way to remove the newline after the output. This would be good for use in a script.

jason@Yog-Sothoth » img » $ tr -dc "[:digit:]" < /dev/urandom | head -c 96
598827488588935359227002896451901115916183070597575997276765531896915172047491309910275008172345

Print only upper case letters.

ason@Yog-Sothoth » img » $ tr -dc "[:upper:]" < /dev/urandom | head -c 96 ; echo
WEZGBFWPHAWCWJYVPMBWKRXLHTILOPUEWLFBCBEWKDPFSIKXMYMSXSRVECWHWISAZWPHLBZIZDREBIRAUUFQIFSFGGLGVTXI

Print a long random string of graphical characters.

jason@Yog-Sothoth » img » $ tr -dc "[:graph:]" < /dev/urandom | head -c 96 ; echo
X2,|Ut2A!V&>h}M9WMb/64&*.aya_O'^.%0x}'MKPK}0(KI8q7'qmc).\dd#!9cYJEal%uBfVkjI7I1.\j(e}R2ls|0DQFi"

Print only a random string of hexadecimal digits.

jason@Yog-Sothoth » img » $ tr -dc "[:xdigit:]" < /dev/urandom | head -c 96 ; echo
6d42253aF8Af0fbFc78c8aa20A33d2DC6AE6987D6593d053D4c9C494EeBb5cBc6ce3DafEFbbD92deAEaFaB2Ea07DAE9c

Include all upper and lowercase letters and numbers.

jason@Yog-Sothoth » img » $ tr -dc "[:alnum:]" < /dev/urandom | head -c 96 ; echo
Y9bW54rVXjDkPecBKFKWSPsW0j0obUFMuqeR2ZI6Mz6NamgPtUHZmrPEoicK33BPcOjyRmesvrgA4mge4POgJnx7CdvCpre1

These simple one-liners can be used to generate random numbers and/or passwords. /dev/urandom is a very good source of random entropy. Not totally random, but good enough for most needs.

Here is another example, using shuf.

jason@Yog-Sothoth » img » $ shuf -i 1-4375674834837535 -n 1
4321886795046917

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.