The_Law

Untitled

Nov 5th, 2018
416
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.88 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. enum
  5. {
  6.     MULTIPLY = 1103515245,
  7.     INCREMENT = 12345,
  8.     MODULE = (1ll << 31)
  9. };
  10.  
  11. typedef struct RandomGenerator
  12. {
  13.     struct RandomOperations *ops;
  14.     unsigned res;
  15. } RandomGenerator;
  16.  
  17. void
  18. destroy(RandomGenerator *rr)
  19. {
  20.     free(rr->ops);
  21.     free(rr);
  22. }
  23.  
  24. unsigned
  25. next(RandomGenerator *rr)
  26. {
  27.  
  28.     return (rr->res * MULTIPLY + INCREMENT) & (MODULE - 1);
  29. }
  30.  
  31. struct RandomOperations
  32. {
  33.     unsigned (*next)(RandomGenerator *rr);
  34.     void (*destroy)(RandomGenerator *rr);
  35. };
  36.  
  37. RandomGenerator *
  38. random_create(int seed)
  39. {
  40.     RandomGenerator *gen = malloc(sizeof(*gen));
  41.     if (gen == NULL) {
  42.         exit(1);
  43.     }
  44.     gen->ops = malloc(sizeof(*(gen->ops)));
  45.     if (gen->ops == NULL) {
  46.         exit(1);
  47.     }
  48.     gen->ops->next = &next;
  49.     gen->ops->destroy = &destroy;
  50.     gen->res = seed;
  51.     return gen;
  52. }
Advertisement
Add Comment
Please, Sign In to add comment