daily pastebin goal
53%
SHARE
TWEET

platform_random.h

mvaganov May 11th, 2015 (edited) 223 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #ifndef __platform_random
  2. #define __platform_random
  3.  
  4. #ifdef _WIN32
  5. #include <time.h>
  6. #define _CLOCK_ clock
  7. #else
  8. #include <sys/time.h>   // wall-clock timer (as opposed to clock cycle timer)
  9. /** In Linux, clock() returns CPU cycles, not wall-clock time. */
  10. inline time_t platform_Clock_NIX() {
  11.     /*
  12.     static timeval g_startTime = { 0, 0 };
  13.     if (g_startTime.tv_sec == 0 && g_startTime.tv_usec == 0)
  14.         gettimeofday(&g_startTime, NULL);   // start the timer
  15.     timeval now;
  16.     time_t seconds, useconds, ms;
  17.     gettimeofday(&now, NULL);
  18.     seconds = now.tv_sec - g_startTime.tv_sec;
  19.     useconds = now.tv_usec - g_startTime.tv_usec;
  20.     ms = (seconds * 1000) + (useconds / 1000);
  21.     return (time_t)ms;
  22.     */
  23.     timeval now;
  24.     gettimeofday(&now, NULL);
  25.     return now.tv_usec;
  26. }
  27.  
  28. #define _CLOCK_ platform_Clock_NIX
  29. #endif
  30.  
  31. inline unsigned char platform_randomBit() {
  32.         static long loops1st, loops2nd;
  33.         static time_t now = _CLOCK_();
  34.         for (now = _CLOCK_(), loops1st = 0; _CLOCK_() == now; ++loops1st);
  35.         for (now = _CLOCK_(), loops2nd = 0; _CLOCK_() == now; ++loops2nd);
  36.         return ((int)(loops2nd < loops1st));   
  37. }
  38.  
  39. /**
  40. * @return relatively random bit sequence. Checks variation in realtime CPU usage.
  41. * Use sparingly!
  42. */
  43. inline unsigned long platform_randomIntTRNG(int a_numBits) {
  44.     // static reduces function overhead & keeps randomness from the last call
  45.     static unsigned long result = 0, loops1st, loops2nd, index = 0, repetitions = 0;
  46.     static char resulta, resultb;
  47.     static time_t now = _CLOCK_();
  48.     for (index = 0; index < a_numBits; ++index) {
  49.         resulta = platform_randomBit();
  50. //*/    // von Neumann's "fair results from a biased coin" algorithm
  51.         //Toss the coin twice.
  52.         resultb = platform_randomBit();
  53.         //If the results match, start over, forgetting both results.
  54.         if(resulta == resultb){ --index; ++repetitions; } else
  55.         //If the results differ, use the first result, forgetting the second.
  56. //*/
  57.         result ^= ((resulta & 1) ^ (repetitions & 1)) << index;
  58.     }
  59.     return result;
  60. }
  61.  
  62. #undef _CLOCK_
  63.  
  64. inline unsigned long * __RandomSeed() {
  65.     static unsigned long g_seed = 5223;
  66.     return &g_seed;
  67. }
  68.  
  69. inline void platform_randomSeed(unsigned long a_seed) { *__RandomSeed() = a_seed; }
  70.  
  71. inline unsigned long platform_random() {
  72.     // Take the current seed and generate a new value
  73.     // from it. Due to our use of large constants and
  74.     // overflow, it would be very hard for someone to
  75.     // predict what the next number is going to be from
  76.     // the previous one.
  77.     //g_seed = (8253729 * g_seed + 2396403);
  78.     platform_randomSeed(8253729 * (*__RandomSeed()) + 2396403);
  79.  
  80.     // return a value between 0 and 2.14 billion (+2^^31)
  81.     return (*__RandomSeed()) & 0x7fffffff;
  82. }
  83.  
  84. inline int platform_randomInt() { return (int)platform_random(); }
  85.  
  86. inline float platform_randomFloat() {
  87.     unsigned long i = platform_random() & 0xffffff;
  88.     return i / (float)(0x1000000);
  89. }
  90.  
  91. inline float platform_randomFloat(float min, float max) {
  92.     float delta = max - min;
  93.     float number = platform_randomFloat() * delta;
  94.     number += min;
  95.     return number;
  96. }
  97.  
  98. #endif
RAW Paste Data
Top