Creep4Play

random.h © Creep4Play

Oct 29th, 2013
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.62 KB | None | 0 0
  1. //random.h © Creep4Play
  2. //random.h - Header-Datei für Zufallszahlen
  3.  
  4. //Überprüfung auf Ersteinbindung
  5. #ifndef _RANDOM_H
  6. #define _RANDOM_H
  7.  
  8. //Header-Dateien
  9. #include <ctime>
  10. #include <functional>
  11. #include <stdlib.h>
  12.  
  13. //Zufalls-Klasse
  14. class rdm
  15. {
  16. private:
  17.     rdm()
  18.     {
  19.         std::hash<unsigned> hash;
  20.         srand(hash((unsigned)time(NULL)));
  21.     }
  22.  
  23. public:
  24.     static int num(int min,int max)
  25.     {
  26.         static rdm seed;
  27.         int cnt = max - min + 1;
  28.         int maxnum = RAND_MAX - (RAND_MAX + 1) % cnt;
  29.         int rdmnum;
  30.  
  31.         do
  32.         {
  33.             rdmnum = rand();
  34.         } while (rdmnum > maxnum);
  35.  
  36.         return rdmnum % cnt + min;
  37.     }
  38. };
  39.  
  40. #endif
Advertisement
Add Comment
Please, Sign In to add comment