8 baytlık bir tane üretmek için iki 4 baytlık rastgele tamsayı birleştirebilirsiniz:
#include
...
uint64_t random =
(((uint64_t) rand() << 0) & 0x00000000FFFFFFFFull) |
(((uint64_t) rand() << 32) & 0xFFFFFFFF00000000ull);
Since rand
returns int
, and sizeof(int) >= 4
on almost any modern platform, this code should work. I've added the << 0
to make the intent more explicit.
The masking with 0x00000000FFFFFFFF
and 0xFFFFFFFF00000000
is to prevent overlapping of the bits in the two numbers in case sizeof(int) > 4
.
DÜZENLEME
@Banthar, RAND_MAX
'ın zorunlu olarak 2 ^ 32
olmadığını ve en az 2 ^ 16
olması gerektiğini düşündüğünden emin olmak için dört 2 baytlık sayıları birleştirin:
uint64_t random =
(((uint64_t) rand() << 0) & 0x000000000000FFFFull) |
(((uint64_t) rand() << 16) & 0x00000000FFFF0000ull) |
(((uint64_t) rand() << 32) & 0x0000FFFF00000000ull) |
(((uint64_t) rand() << 48) & 0xFFFF000000000000ull);