Skip to content

Instantly share code, notes, and snippets.

@gpr
Created January 12, 2015 10:50
Show Gist options
  • Save gpr/5f08a40d7bb3bd80ba78 to your computer and use it in GitHub Desktop.
Save gpr/5f08a40d7bb3bd80ba78 to your computer and use it in GitHub Desktop.
Generate random bytes with Ruby
# Nothing to require
# You can define your seed or generate one using system random function
seed = srand
prng = Random.new(seed)
# Generate X bytes
X = 16
bin_str = prng.bytes(X)
# Get the hexadecimal string
hex_str = bin_str.unpack('H*')[0]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment