Instead of rand()%2 try rand()>(RAND_MAX/2) . You can assume that rand() is uniform on the interval [0, RAND_MAX] .
Edit: This was suggested by Shahbaz in the comments, which I noticed only after I posted this answer.
Edit: ArjunShankar called me according to my previous wording: "rand () is set only as a single on the interval [0, RAND_MAX]"
From the C99 standard:
The rand function computes a sequence of pseudo-random integers ranging from 0 to RAND_MAX.
Technically, uniformity (or equidistributed) is not specified, but is the de facto standard used for implementations of the commonly used PRNG (e.g., Mersenne Twister). This allows the programmer to easily create their own PRNG with uneven distribution. Without this property, the programmer is forced to implement his own PRNG from scratch.
source share