Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * Windows: gcc
- * POSIX: gcc
- */
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <time.h>
- // Windows 32-bit and 64-bit
- #ifdef _WIN32
- #include <Windows.h>
- #include <wincrypt.h>
- #include <conio.h>
- unsigned long int rng() {
- static HCRYPTPROV prov;
- if (CryptAcquireContext(&prov, NULL, NULL, PROV_RSA_FULL, 0)) {
- unsigned long int li = 0;
- if (CryptGenRandom(prov, sizeof(li), (BYTE *) &li)) {
- return li;
- } else {
- fprintf(stderr, "error: CryptGenRandom()\n");
- return rand();
- }
- }
- }
- // Linux or MacOS
- #else
- #include <fcntl.h>
- #include <termios.h>
- #define rng() random()
- char getch() {
- char c;
- struct termios t;
- tcgetattr(0, &t);
- t.c_lflag &= ~(ECHO|ICANON);
- tcsetattr(0, TCSANOW, &t);
- c = getchar();
- tcsetattr(0, TCSADRAIN, &t);
- return c;
- }
- int kbhit() {
- int c, of;
- struct termios ot, nt;
- tcgetattr(0, &ot);
- nt = ot;
- nt.c_lflag &= ~(ECHO|ICANON);
- tcsetattr(0, TCSANOW, &nt);
- of = fcntl(0, F_GETFL, 0);
- fcntl(0, F_SETFL, of | O_NONBLOCK);
- c = getchar();
- tcsetattr(0, TCSANOW, &ot);
- fcntl(0, F_SETFL, of);
- if (c != EOF) {
- ungetc(c, stdin);
- return 1;
- }
- return 0;
- }
- #endif
- int main(void) {
- // Random
- #ifdef _WIN32
- #else
- struct timespec ts;
- if (timespec_get(&ts, TIME_UTC) == 0) {
- fprintf(stderr, "error: timespec_get()\n");
- return 1;
- }
- srandom(ts.tv_nsec ^ ts.tv_sec);
- #endif
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement