The simplest self-made version, which works with almost all compilers, except, for some reason, Intel C 15. It is based on the IEEE-754 format. We generate the denominator D as a denormalized number, whose mantissa is 2 32 . The numerator r is generated, which is also denormalized and less than 2 32 (it is not equal to zero). Then divide r by D, guaranteed to get a number between 0 and 1. Then, depending on the sign of s, we make it either positive or negative. Instead of generating s like I can (and even better) use some other number generator from the range from 0 to 2 32 -1 (just NOT rand (), because it has a different range), because this linear generator is not very stable, alternates parity of numbers and repeats through a full cycle.
Unfortunately, the code is machine-dependent, it depends on the order of the bytes in the word and on the IEEE-754 format, therefore it is suitable only for handicraft work, but not for serious projects.
#include <stdio.h> double r; unsigned int s = 17; static double get_random ( ) { double D = 0.0; do s = 19993*s+1; while (s==0); // s != 0 *((unsigned int*)&D+1) = 1; // D = 0x0000000100000000 r = 0.0; *(unsigned int *)&r = s; // r = 0x00000000[ s ] r /= D; // r < 1.0 if (s&1) r = -r; return r; } int main() { int i; for (i = 0; i<100; i++) printf ("%lf\n", get_random()); return 0; }