Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <unistd.h>
- #include <stdio.h>
- #include <sys/wait.h>
- #include <stdlib.h>
- enum
- {
- MUL = 1103515245U,
- INC = 12345U,
- MOD = 2147483648U
- };
- typedef struct RandomGenerator RandomGenerator;
- static int next(RandomGenerator *rr);
- static void destroy(RandomGenerator *rr);
- typedef struct RandomOperations
- {
- int (*next) (RandomGenerator *rr);
- void (*destroy) (RandomGenerator *rr);
- } RandomOperations;
- static const RandomOperations ops = {&next, &destroy};
- typedef struct RandomGenerator
- {
- unsigned int current;
- const struct RandomOperations *ops;
- } RandomGenerator;
- static int next(RandomGenerator *rr)
- {
- unsigned int cur = rr->current;
- cur = cur * MUL % MOD + INC % MOD;
- rr->current = cur;
- return cur;
- }
- static void destroy(RandomGenerator *rr)
- {
- if (rr == NULL) {
- return;
- }
- free(rr);
- }
- RandomGenerator *random_create(int seed)
- {
- RandomGenerator *rr = calloc(1, sizeof(*rr));
- if (rr == NULL) {
- return NULL;
- }
- rr->current = seed;
- rr->ops = &ops;
- return rr;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement