8

I'm looking for ways to use /dev/random (or /dev/urandom) from the command line. In particular, I'd like to know how to use such a stream as stdin to write streams of random numbers to stdout (one number per line).

I'm interested in random numbers for all the numeric types that the machine's architecture supports natively. E.g. for a 64-bit architecture, these would include 64-bit signed and unsigned integers, and 64-bit floating point numbers. As far as ranges go, the maximal ranges for the various numeric types will do.

I know how to do all this with all-purpose interpreters like Perl, Python, etc., but I'd like to know how to do this with "simpler" tools from the shell. (By "simpler" I mean "more likely to be available even in a very minimal Unix installation".)

Basically the problem reduces that of converting binary data to their string representations on the command line. (E.g., this won't do: printf '%f\n' $(head -c8 /dev/random).)

I'm looking for shell-agnostic answers. Also, the difference between /dev/random and /dev/urandom is not important for this question. I expect that any procedure that works for one will work for the other, even when the semantics of the results may differ.


I adapted EightBitTony's answer to produce the functions toints, etc. shown below.

Example use:

% < /dev/urandom toprobs -n 5
0.237616281778928
0.85578479125532
0.0330049682019756
0.798812391655243
0.138499033902422

Remarks:

  1. I'm using hexdump instead of od because it gave me an easier way to format the output the way I wanted it;
  2. Annoyingly though, hexdump does not support 64-bit integers (wtf???);
  3. The functions' interface needs work (e.g. they should accept -n5 as well as -n 5), but given my pitiful shell programming skillz, this was the best I could put together quickly. (Comments/improvements welcome, as always.)

The big surprise I got from this exercise was to discover how hard it is to program on the shell the most elementary numerical stuff (e.g. read a hexadecimal float, or get the maximum native float value)...


_tonums () {
  local FUNCTION_NAME=$1 BYTES=$2 CODE=$3
  shift 3

  local USAGE="Usage: $FUNCTION_NAME [-n <INTEGER>] [FILE...]"
  local -a PREFIX

  case $1 in
    ( -n ) if (( $# > 1 ))
           then
               PREFIX=( head -c $(( $2 * $BYTES )) )
               shift 2
           else
               echo $USAGE >&2
               return 1
           fi ;;
    ( -* ) echo $USAGE >&2
           return 1 ;;
    (  * ) PREFIX=( cat ) ;;
  esac

  local FORMAT=$( printf '"%%%s\\n"' $CODE )
  $PREFIX "$@" | hexdump -ve $FORMAT
}

toints () {
  _tonums toints 4 d "$@"
}

touints () {
  _tonums touints 4 u "$@"
}

tofloats () {
  _tonums tofloats 8 g "$@"
}

toprobs () {
  _tonums toprobs 4 u "$@" | perl -lpe '$_/=4294967295'
}
  • 1
    tr -cs '[:digit:]' '[\n*]' </dev/urandom should give you integer number only. – cuonglm Mar 10 '16 at 15:59
  • as far as I am aware, /dev/random do not generate random numbers per se, but add noise to the mathematical functions that give pseudo-random number for them to become less predictable. You might want to look at randomlib.sourceforge.net – Rui F Ribeiro Mar 10 '16 at 16:01
  • 1
  • Do you need crypto-quality randomness, or is e.g. seeding based on the time good enough? – Gilles Mar 10 '16 at 22:45
  • @Gilles: the motivation for my question was more to find out about shell and Unix tools than about randomness; I just wanted to know what basic Unix tools existed for converting a stream of binary data into their string representation. – kjo Mar 11 '16 at 1:36
14

You can use od to get numbers out of /dev/random and /dev/urandom.

For example,

2 byte unsigned decimal integers,

$ od -vAn -N2 -tu2 < /dev/urandom
24352

1 byte signed decimal integer,

$ od -vAn -N1 -td1 < /dev/urandom
-78

4 byte unsigned decimal integers,

$ od -vAn -N4 -tu4 < /dev/urandom
3394619386

man od for more information on od.

  • 1
    Beware that in VMs, /dev/random can give you limited input. The entropy generator depends on noise sampled from hardware activity as far as I remember. – Rui F Ribeiro Mar 10 '16 at 16:29
  • 1
    @RuiFRibeiro yes but the original question clearly states they're not worried about the behaviour of /dev/urandom or /dev/random, only how to collect output from it. – EightBitTony Mar 10 '16 at 16:53
5

Some shells (e.g. bash(1)) have a $RANDOM "variable" that gives random numbers.

1

You could do something like:

perl -le '
  while (q(
    c char,  C unsigned char, s! short, S! unsigned short,
    i! int,  I! unsigned int, l! long,  L! unsigned long,
    f float, d double,) =~ /(\S+) (.*?),/gs) {
    $size = length(pack $1, 0);
    sysread STDIN, $data, $size;
    print "$2($size): " . unpack($1, $data);
  }' < /dev/urandom

Which on a 64 bit system would give you something like:

char(1): -98
unsigned char(1): 62
short(2): -12526
unsigned short(2): 399
int(4): 499066219
unsigned int(4): 2312134917
long(8): -4889591208978026255
unsigned long(8): 2080566823379835456
float(4): 55.4727554321289
double(8): 8.6395690272822e-05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.