I would simply define a char: char b = rand(); i think this should be
enough.
This might work, or it might fail badly. Plain char may be either
signed or unsigned. Its size is 8 bits in most implementations, but
the standard only guarantees that it's at least 8 bits. The value of
RAND_MAX is at least 32767. Assigning a value larger than CHAR_MAX,
or smaller than CHAR_MIN, to a char object produces an overflow, which
(if char is signed) invokes undefined behavior; it typically grabs the
low-order bits, but this isn't guaranteed. Finally, the low-order
bits of the results returned by rand() are unreliable in many
implementations.
The OP wants to generate random numbers is a specified range (0..255).
Currently i have the problem that i want to have a true 32 bit random
number with rand(). How can i do that, RAND_MAX is not changeable....
RAND_MAX is guaranteed to be at least 32767, so rand() gives you at
least 15-bit random nubers. Call rand() 3 times and use bitwise
arithmetic to fill in your 32 bits. For example:
uint32_t result;
result = rand() & 0x7fff; /* 15 bits */
result <<= 15;
result |= rand() & 0x7fff; /* 15 bits */
result <<= 2;
result |= rand() & 0x0003; /* 2 bits */
If RAND_MAX happens to be at least 65535, you can call rand() only
twice.
If you don't trust the low-order bits of the results returned by
rand(), you might generate, say, 8 bits at a time using the method
described in question 13.16 of the C FAQ.
In many systems, rand() isn't very good, but there may be other random
number generators that give better results. If you don't mind writing
non-portable code, searching your documentation for "drand48" might be
fruitful; on some systems, /dev/random is a good source of random
bits. Since this is system-specific, the details are off-topic here.