Advertisement
pouar

randu.c

Nov 10th, 2015
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.89 KB | None | 0 0
  1. /*
  2. randu.c - a bad implementation of a bad algorithm
  3. see http://en.wikipedia.org/wiki/RANDU
  4. scruss - 2013-06-02
  5. compiles rather nicely with:
  6. gcc -std=c99 -pedantic -O2 -Wall -o randu randu.c
  7. */
  8. #include <stdio.h>
  9. #include <stdlib.h>
  10. #include <inttypes.h>
  11.  
  12. static uint32_t randu(uint32_t a)
  13. {
  14.     return ((uint64_t) a * 65539UL) % 2147483648UL;
  15. }
  16.  
  17. int main(int argc, char *argv[])
  18. {
  19.     uint32_t a;
  20.     unsigned int seed;
  21.     if (argc != 2) {
  22.         fprintf(stderr, "usage: %s seed\n", argv[0]);
  23.         exit(EXIT_FAILURE);
  24.     }
  25.     else {
  26.         if (sscanf(argv[1], "%ui", &seed) != 1) {
  27.             (void) fputs("error - seed must be an integer\n", stderr);
  28.             exit(EXIT_FAILURE);
  29.         }
  30.         /* initial seed must be odd */
  31.         if (seed % 2 == 0) {
  32.             seed--;
  33.         }
  34.         a = randu((uint32_t) seed);
  35.         for (;;) {
  36.             (void) fwrite(&a, sizeof(a), (size_t) 1, stdout);
  37.             a = randu(a);
  38.         }
  39.     }
  40.     /* will never be reached */
  41. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement