Ranval is a small tool I wrote to generate random numbers on the command line. It can either print them to standard output (the default) or run in quiet mode and return the random value as the exit code of the process (this limits the range of possible random values ranval can return). It offers more flexibility than your shell's RANDOM variable (at the cost of a fork and exec).

It was written on OpenBSD and uses library functions written by OpenBSD developers (more specifically, it uses the arc4random(3) family of functions). If your operating system also supports these functions, chances are that porting ranval is not difficult. If you succeed, please let me know if you had to make any changes so I can see about incorporating them in a new version (my e-mail address is in the source). You can download the latest version of ranval here (ranval-1.4.tar.gz [2KB]).

The following table lists all releases and their changes. All previous versions are still available for download:

VersionRelease dateChanges
1.4 2016-04-28  • pledge(2) ranval
1.3 2014-04-17  • Minor cleanup now that mandoc is the default in OpenBSD
1.2 2011-10-01  • The upper bound can now be set via environment variable RANVAL_BOUND
 • Allow bound of 232 to be specified on the command line
 • Allow up to 263 - 1 iterations
1.1 2010-12-06  • FreeBSD support
 • Minor manpage clarification
1.0 2010-04-05  • Initial release

As of OpenBSD 5.6, this program is also available in the OpenBSD ports tree, you can find it in sysutils/ranval. If you wish to install it, make sure your PKG_PATH is properly set and simply issue:

sudo pkg_add ranval

RANVAL(1)                  OpenBSD Reference Manual                  RANVAL(1)

NAME
     ranval - generate random numbers

SYNOPSIS
     ranval [-q | -c count] [[-b] bound]

DESCRIPTION
     ranval returns a random integer value between 0 and bound - 1, inclusive.
     The default value for bound is 4294967296 (2^32).  By default, these
     values are written to stdout.  ranval uses the arc4random(3) family of
     functions to provide good random numbers without a modulo bias.

     The options are as follows:

     -q      If the -q option is specified, instead of printing ranval returns
             a random exit value of 0 to bound - 1, inclusive.  In this case,
             bound will be capped at 256.

     -c count
             Generate count random numbers before exiting.  Use 0 to produce
             random numbers until interrupted.  By default, only 1 random
             number will be generated.  Count is limited to
             9223372036854775807 (2^63 - 1).

     bound | -b bound
             Specify an upper bound.  Random values between 0 and bound - 1,
             inclusive will be generated.  This value must be between 1 (which
             results in debianesque randomness) and 4294967296 (the default).
             The bound can also be given in the environment variable
             RANVAL_BOUND.  When both the environment variable is set and a 
             bound is specified on the command line, the latter takes
             precedence.

SEE ALSO
     arc4random(3)

OpenBSD 5.5                      April 5, 2010                     OpenBSD 5.5

This software is distributed under liberal terms, modeled after the ISC license:
Copyright (c) 2010, 2011, 2014 Paul de Weerd 

Permission to use, copy, modify, and distribute this software for any
purpose with or without fee is hereby granted, provided that the above
copyright notice and this permission notice appear in all copies.

THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.

© 2010, 2011, 2014, 2016 Paul 'WEiRD' de Weerd Powered by OpenBSD