Kumar Roshan Mehta
Kumar Roshan Mehta

Reputation: 3316

generate array of random 16 byte in hex

I want to generate array of 16 byte in hex like this in C or C++11:

const uint8_t alice[16] = { 0x0, 0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7, 0x8, 0x9, 0xa, 0xb, 0xc, 0xd, 0xe, 0xf };

How can I do that? A sample code would be most appreciated.

Upvotes: 0

Views: 633

Answers (1)

jspcal
jspcal

Reputation: 51914

Try using std::independent_bits_engine to perform the extraction:

// Array of random chars
std::vector<unsigned char> chars(16);

// Random bits generator
std::independent_bits_engine<std::default_random_engine, CHAR_BIT, unsigned char> engine;

// Generate the array
std::generate(begin(chars), end(chars), std::ref(engine));

Upvotes: 1

Related Questions