Advertisement
Guest User

Untitled

a guest
Jan 19th, 2019
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.86 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdint.h>
  3. #include <stdlib.h>
  4.  
  5. struct RandomGenerator;
  6.  
  7. typedef struct Operations
  8. {
  9.     void (*destroy)(struct RandomGenerator*);
  10.     int (*next)(struct RandomGenerator*);
  11. } Operations;
  12.  
  13. typedef struct RandomGenerator
  14. {
  15.     int current;
  16.     int a;
  17.     int c;
  18.     int mod;
  19.     Operations* ops;
  20. } RandomGenerator;
  21.  
  22. void destroy(RandomGenerator *gen) {
  23.     free(gen);
  24. }
  25.  
  26. int next(RandomGenerator *gen) {
  27.     return gen->current = (((__int64_t) gen->a * gen->current + gen->c) & gen->mod);
  28. }
  29.  
  30. Operations operations = {destroy, next};
  31.  
  32. RandomGenerator *random_create(int seed) {
  33.     RandomGenerator* gen = malloc(sizeof(RandomGenerator));
  34.     gen->current = seed;
  35.     gen->a = 1103515245;
  36.     gen->c = 12345;
  37.     gen->mod = (1LL << 31) - 1;
  38.     gen->ops = &operations;
  39.     return gen;
  40. }
  41.  
  42. int main() {
  43.     return 0;
  44. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement