random.c

Go to the documentation of this file.
00001 
00024 #include <stdlib.h>
00025 #include <SDL.h>
00026 #include <time.h>
00027 
00028 #ifndef WIN32
00029 
00033 static FILE *urand = NULL;
00034 #endif                                                                       
00035 
00036 void RandInit() {
00037     #ifndef WIN32
00038     // see if /dev/urandom (pretty common on UNIX-like systems) has already
00039     // been opened, and if not, open it
00040     if ((urand == NULL) && ((urand = fopen("/dev/urandom", "rb")) == NULL))
00041     #endif
00042     {
00043         // couldn't open /dev/urandom, or on systen that doesn't support it
00044         srand((unsigned int)time(NULL));
00045     }
00046 }
00047 
00048 Uint32 RandNum() {
00049     #ifndef WIN32
00050     // check for already open file handle to /dev/urandom
00051     if (urand) {
00052         int num;
00053         // read in the next 4 bytes of randomness
00054         fread(&num, 4, 1, urand);
00055         return num;
00056     }
00057     #endif
00058     // use the ANSI C interface to a pseudo random number generator
00059     return (rand() << 16) | rand();
00060 }
00061 
00062 void RandUninit() {
00063     #ifndef WIN32
00064     // check for already open file handle to /dev/urandom
00065     if (urand) {
00066         // close the file
00067         fclose(urand);
00068     }
00069     #endif
00070 }
00071 

Generated on Mon May 28 04:41:39 2007 for Retro Tank Super Attack by  doxygen 1.5.2