Advertisement
Guest User

Untitled

a guest
Oct 24th, 2019
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.49 KB | None | 0 0
  1. /*
  2. * Windows: gcc
  3. * POSIX: gcc
  4. */
  5.  
  6. #include <stdio.h>
  7. #include <stdlib.h>
  8. #include <string.h>
  9. #include <time.h>
  10.  
  11. // Windows 32-bit and 64-bit
  12. #ifdef _WIN32
  13. #include <Windows.h>
  14. #include <wincrypt.h>
  15. #include <conio.h>
  16.  
  17. unsigned long int rng() {
  18. static HCRYPTPROV prov;
  19. if (CryptAcquireContext(&prov, NULL, NULL, PROV_RSA_FULL, 0)) {
  20. unsigned long int li = 0;
  21. if (CryptGenRandom(prov, sizeof(li), (BYTE *) &li)) {
  22. return li;
  23. } else {
  24. fprintf(stderr, "error: CryptGenRandom()\n");
  25. return rand();
  26. }
  27. }
  28. }
  29.  
  30. // Linux or MacOS
  31. #else
  32. #include <fcntl.h>
  33. #include <termios.h>
  34.  
  35. #define rng() random()
  36.  
  37. char getch() {
  38. char c;
  39. struct termios t;
  40. tcgetattr(0, &t);
  41. t.c_lflag &= ~(ECHO|ICANON);
  42. tcsetattr(0, TCSANOW, &t);
  43. c = getchar();
  44. tcsetattr(0, TCSADRAIN, &t);
  45. return c;
  46. }
  47.  
  48. int kbhit() {
  49. int c, of;
  50. struct termios ot, nt;
  51. tcgetattr(0, &ot);
  52. nt = ot;
  53. nt.c_lflag &= ~(ECHO|ICANON);
  54. tcsetattr(0, TCSANOW, &nt);
  55. of = fcntl(0, F_GETFL, 0);
  56. fcntl(0, F_SETFL, of | O_NONBLOCK);
  57. c = getchar();
  58. tcsetattr(0, TCSANOW, &ot);
  59. fcntl(0, F_SETFL, of);
  60. if (c != EOF) {
  61. ungetc(c, stdin);
  62. return 1;
  63. }
  64. return 0;
  65. }
  66. #endif
  67.  
  68. int main(void) {
  69.  
  70. // Random
  71. #ifdef _WIN32
  72. #else
  73. struct timespec ts;
  74. if (timespec_get(&ts, TIME_UTC) == 0) {
  75. fprintf(stderr, "error: timespec_get()\n");
  76. return 1;
  77. }
  78. srandom(ts.tv_nsec ^ ts.tv_sec);
  79. #endif
  80.  
  81. return 0;
  82. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement