randgen is a tool which generates random bytes using a pseudorandom number generator (PRNG).
cargo install randgen
The release page contains pre-built binaries for Linux, macOS and Windows.
Please see BUILD.adoc.
Generate 1 KiB of random bytes:
randgen 1KiB
The following output formats are available:
raw
(encode the generated bytes as raw bytes)base64
(encode the generated bytes as base64)base64url
(encode the generated bytes as URL-safe base64)hex
(encode the generated bytes as hex string)
Tip
The result is output on a single line, so there is no line wrapping.
The default output format is raw
. To change this, use -f
option.
randgen -f base64 256
This has the same result as:
randgen 256 | base64 -w 0
Supported PRNGs are:
- ISAAC RNGs (if enabled at compile time)
- Mersenne Twister RNGs (if enabled at compile time)
- PCG RNGs (if enabled at compile time)
- SFC RNGs (if enabled at compile time)
- Xorshift family
- SplitMix64 RNG
- Xorshift RNG (if enabled at compile time)
- xoroshiro RNGs
- xoshiro RNGs
Supported CSPRNGs are:
The default RNG is chacha12
. To change this, use -r
option.
randgen -r pcg64 "2 MB"
-s
option allows you to specify a 64-bit unsigned integer random seed to be
used when creating a new PRNG. If this option is not specified, the RNG seeded
via random data from system sources such as the getrandom
system call on
Linux.
$ randgen -f hex -r sfc32 -s 8 32B
24f48cd0c3f6a1c6e8d7b4dcff9578864aced749e4eb1805dfba8b6e21d0cba0
This has the same result as:
$ randgen -r sfc32 -s 8 32B | xxd -c 0 -p
24f48cd0c3f6a1c6e8d7b4dcff9578864aced749e4eb1805dfba8b6e21d0cba0
--generate-completion
option generates shell completions to standard output.
The following shells are supported:
bash
elvish
fish
nushell
powershell
zsh
Example:
randgen --generate-completion bash > randgen.bash
To encode the generated bytes into an output format not supported by randgen
,
pipe the output of randgen
to the input of another program.
For example, to encode the generated bytes as base32:
$ randgen -s 16 32B | base32
DYQZ33GRHUR5WIFRSBZVQ3ZPEFZASXB3SNTRUHR3NPITZBMDPZXQ====
randgen
does not wrap lines of the result.
To wrap lines of the result:
$ randgen -f base64url -s 256 128B | fold -w 76
0X82yjVU1_JLdDU07ywJ_7CAJBwRTEVS_i1-kRgR6HQsb9jFyQNuiFIBmrTucpwt-CZjZj90JYjE
6D2erJEn6f8ju9KBmLraVj55I9KQRxhh2BmJMrovQhEK9nU3Ysn-mOURovtVCE45dqKyWTHuWLV2
Hr1yramxGwVm88LXhA8=
This has the same result as:
$ randgen -s 256 128B | basenc --base64url
0X82yjVU1_JLdDU07ywJ_7CAJBwRTEVS_i1-kRgR6HQsb9jFyQNuiFIBmrTucpwt-CZjZj90JYjE
6D2erJEn6f8ju9KBmLraVj55I9KQRxhh2BmJMrovQhEK9nU3Ysn-mOURovtVCE45dqKyWTHuWLV2
Hr1yramxGwVm88LXhA8=
The following generates 64 KiB of random bytes and encodes it as base64:
randgen -f base64 64KiB
This section describes how to do the same thing with other things.
openssl rand
can be used for this purpose.
openssl rand -base64 64K
gpg --gen-random
can be used for this purpose.
gpg --gen-random 1 65536 | base64
random(4)
(/dev/random
and /dev/urandom
) can be used for this purpose.
head -c 65536 /dev/random | base64
Caution
Unlike randgen
, these things wrap lines of the result.
Please see the following:
The upstream repository is available at https://github.com/sorairolake/randgen.git.
Please see CHANGELOG.adoc.
Please see CONTRIBUTING.adoc.
Copyright (C) 2025 Shun Sakai (see AUTHORS.adoc)
- This program is distributed under the terms of either the Apache License 2.0 or the MIT License.
- Some files are distributed under the terms of the Creative Commons Attribution 4.0 International Public License.
This project is compliant with version 3.2 of the REUSE Specification. See copyright notices of individual files for more details on copyright and licensing information.