Generate 16 bytes of random numbers in C/C++ [duplicate]

  c++, random

I need to write a program to generate a key containing 16 bytes of random numbers. I wrote the code using the rand() function to get a 4 bytes random number and got the resulting value by concatenating the rand() 4 times in a loop. But I didn’t get the result I wanted. Also I read somewhere that the rand() function does not necessarily creates a 4-bytes number. So I wanted to know if there is any other way I can generate the key.

#include <stdio.h>
#include <string.h>

int main(void) {
   unsigned char data[16];
   unsigned long VAL = 0;

   for(i = 0; i < 4; i++) {
      unsigned int num = rand();
      VAL = VAL + num;
   }
  printf("%xn", VAL);
  memset(&data, VAL, 16);
}

Source: Windows Questions C++

LEAVE A COMMENT