Mtwister
A pure C implementation of the Mersenne twister is a pseudo-random number generation algorithm.
Install / Use
/learn @ESultanik/MtwisterREADME
The MTwister C Library
The Mersenne twister is a pseudo-random number generation algorithm that was developed in 1997 by Makoto Matsumoto (松本 眞) and Takuji Nishimura (西村 拓 士). Although improvements on the original algorithm have since been made, the original algorithm is still both faster and "more random" than the built-in generators in many common programming languages (C and Java included).
There are already many implementations of the algorithm, so why did I write one myself? There are a number of reasons:
- the pseudocode on Wikipedia suggested that it was relatively simple to implement;
- I never trust pseudocode on Wikipedia, so I wanted an implementation from the original academic paper; and
- it would have been just as easy to implement it myself than to figure out the API of someone else's ugly C code.
Note that this code has not been tested to a sufficient extent to be confidently used for cryptography.
Example
The following code will create a new random generator seeded at <code>1337</code> and print out one thousand random doubles between 0 and 1.
#include <stdio.h>
#include "mtwister.h"
int main() {
MTRand r = seedRand(1337);
int i;
for(i=0; i<1000; i++) {
printf("%f\n", genRand(&r));
}
return 0;
}
License
Public domain. Have fun!
Related Skills
node-connect
338.7kDiagnose OpenClaw node connection and pairing failures for Android, iOS, and macOS companion apps
frontend-design
83.6kCreate distinctive, production-grade frontend interfaces with high design quality. Use this skill when the user asks to build web components, pages, or applications. Generates creative, polished code that avoids generic AI aesthetics.
openai-whisper-api
338.7kTranscribe audio via OpenAI Audio Transcriptions API (Whisper).
commit-push-pr
83.6kCommit, push, and open a PR
