This gist contains implementations of the MMIX linear congruential generator in Python and C.
Created
January 23, 2026 20:47
-
-
Save phoenixthrush/5638ac45183f105003649b6a95753728 to your computer and use it in GitHub Desktop.
Random Number Generation: MMIX LCG in Python and C #RandomNumberGeneration #LinearCongruentialGenerator #MMIX
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // clang -Wall -Wextra -Werror -O2 -pedantic -o main main.c | |
| #include <stdint.h> | |
| #include <stdio.h> | |
| uint64_t lcg_next(uint64_t *seed) { | |
| uint64_t a = 6364136223846793005; | |
| uint64_t c = 1442695040888963407; | |
| *seed = (a * *seed + c) % UINT64_MAX; | |
| return *seed; | |
| } | |
| int main(void) { | |
| uint64_t seed = 12345; | |
| for (int i = 0; i < 10; i++) { | |
| printf("%016llx\n", lcg_next(&seed)); | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class MMIX: | |
| """Linear Congruential Generator""" | |
| A = 6364136223846793005 | |
| C = 1442695040888963407 | |
| def __init__(self, seed): | |
| self.seed = seed | |
| def next(self): | |
| """Updates seed and returns the new value""" | |
| self.seed = (self.A * self.seed + self.C) % 2**64 | |
| return self.seed | |
| def main(): | |
| mmix = MMIX(12345) | |
| for _ in range(10): | |
| print(f"{mmix.next():016x}") | |
| if __name__ == "__main__": | |
| main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment