Skip to content

Instantly share code, notes, and snippets.

@kini
Last active June 7, 2016 12:47
Show Gist options
  • Save kini/20a0e853a2c32d20310e1484a0a90971 to your computer and use it in GitHub Desktop.
Save kini/20a0e853a2c32d20310e1484a0a90971 to your computer and use it in GitHub Desktop.
better random numbers for bash
random () {
# note: $RANDOM only gives 15 bits of entropy, and the apparently
# commonly used $RANDOM$RANDOM is non-uniform as it misses such
# numbers as 4294967295. This command will grab 4 bytes from
# /dev/urandom, interpret them as a 4-byte unsigned integer, and
# format them in decimal representation. It can be used as a
# replacement for $RANDOM when you want a full 32 bits of entropy.
od -An -t u4 -N4 < /dev/urandom | tr -d '[[:space:]]'
}
export -f random
@jsyeo
Copy link

jsyeo commented Jun 7, 2016

Use od -An -t u4 -N4 < /dev/urandom | tr -d '[[:space:]]' if you find the additional whitespace annoying.

@kini
Copy link
Author

kini commented Jun 7, 2016

Thanks, good point! Might as well throw that in.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment