gpr
1/12/2015 - 10:50 AM

Generate random bytes with Ruby

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]