vendredi 9 mars 2018

Efficient way to generate random 32-bit unsigned integer in C?

I need to generate random 32-bit value as a uint32 using C.

I do this, but I'm not sure it's efficient way

#define RAND() (rand() & 0x7fff)  /* ensure only 15-bits */
uint32_t n = ((uint32_t)RAND() << 26)  ^ ((uint32_t)RAND() << 13) ^ (uint32_t)RAND();
printf("%u", n);




Aucun commentaire:

Enregistrer un commentaire