Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * mkrnd - generate random number
- */
- #include <stdio.h>
- #include <stdlib.h>
- #include <unistd.h>
- #include <stdint.h>
- #include <string.h>
- #include <sys/time.h>
- #include <sys/types.h>
- #include <sys/stat.h>
- #include <fcntl.h>
- static uint32_t randrange(uint32_t s, uint32_t d)
- {
- uint32_t c;
- int f = -1;
- struct timeval t; memset(&t, 0, sizeof t);
- if (d < s) return s;
- #if 0
- f = open("/dev/urandom", O_RDONLY);
- if (f == -1) f = open("/dev/random", O_RDONLY);
- if (f == -1) goto _rand;
- read(f, &c, sizeof(uint32_t));
- close(f);
- goto _norand;
- _rand:
- #endif
- gettimeofday(&t, NULL);
- c ^= (((t.tv_sec * t.tv_usec) * t.tv_sec) << 15) ^ t.tv_usec;
- srandom(c);
- c = random();
- #if 0
- _norand:
- #endif
- c = c % (d - s + 1 == 0 ? d - s : d - s + 1) + s;
- return c;
- }
- static uint64_t randrangel(uint64_t s, uint64_t d)
- {
- uint64_t c;
- c = randrange(0, (uint32_t)0xffffffff);
- c <<= 32;
- c |= randrange(0, (uint32_t)0xffffffff);
- c = c % (d - s + 1 == 0 ? d - s : d - s + 1) + s;
- return c;
- }
- int main(int argc, char **argv)
- {
- uint64_t x;
- if (argc < 4) return 1;
- for (x = 0; x < atoll(*(argv+3)); x++)
- printf("%lld\n", randrangel(atoll(*(argv+1)), atoll(*(argv+2))));
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment