Skip to content

Instantly share code, notes, and snippets.

@phoenixthrush
Created January 23, 2026 20:47
Show Gist options
  • Select an option

  • Save phoenixthrush/5638ac45183f105003649b6a95753728 to your computer and use it in GitHub Desktop.

Select an option

Save phoenixthrush/5638ac45183f105003649b6a95753728 to your computer and use it in GitHub Desktop.
Random Number Generation: MMIX LCG in Python and C #RandomNumberGeneration #LinearCongruentialGenerator #MMIX
// 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));
}
}
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