// rand.c: Minimalistic random Byte generator. A quicker and dirtier replacement // for /dev/urandom, because script alternatives like // while true; do date +%s%N > date.tmp; sha512sum date.tmp >> foo.bar0; done // with about 100 kB/s and // while true; do echo $RANDOM >> foo.bar1; done // with about 500 kB/s are much slower than /dev/urandom, which has about 3 MB/s // with a 32 Bit kernel and 6 MB/s with a 64 Bit kernel. // And for production of equally distributed bytes the filtering of the random numbers // (for the first example) and conversion to binary is missing. // An alternative is // openssl rand -out foo.bar2 100000000000000 // with a speed of about 20 MB/s. // // Compile this program e. g. with // cc -Wall -O3 -lm -o rand rand.c // // Example usage: // ./rand > file.bin // Speed: About 50 MByte/s with a 2.2 GHz CPU and a HDD with 50 MB/s. // Tuning with a printf which uses more than one Byte or using a union // did not work; it did not get faster. // // Dr. Rolf Freitag 2009, License: GPL, Version 1.0 #include #include #include #include #include #include int main() { int i = time(NULL); srand( (getuid() << 15) ^(getppid() << 10) ^(getpid() << 5) ^((int)sqrt(i))); for(;;) { i^=rand(); printf("%c",(char)i); printf("%c",(char)(i>>8)); } return 0; }