mardi 16 novembre 2021

rand() Function to give a 32-bit random unsigned int

I have some trouble coding a Function that takes 3 rand()- values and generates a 32-bit value. Specifically it has to be 15-bit from the first rand() operation; then attaching another 15-bit from the second rand() operation; and lastly attaching 2-bits from the last rand() operation;

My idea was something like this

unsigned int x = (rand()<<17 | rand()<<2 ) | rand()>>13;

However i don´t think this function gives me values of the entire unsigned int range, my guess it has something to do with the fact that rand() gives you at least a value of 32767 (as far as i understand).

I hope someone can help me out.

Cheers




Aucun commentaire:

Enregistrer un commentaire