I need a very simple pseudo random generator (no any specific quality requiremen
ID: 654241 • Letter: I
Question
I need a very simple pseudo random generator (no any specific quality requirements) and I found Microsoft's variant of LCG algorithm used for rand() C runtime library function fit my needs (gcc's one seems too complex).
However, I worry the algorithm (including its "magic numbers" i.e coefficients) may by patented or restricted for use in some another way.
Is it allowed to use this algorithm without any licence or patent restrictions or not?
I can't use library rand() because I need my results to be exactly reproducible on different platforms
Explanation / Answer
The Mersenne twister prng is well known and implemented.
For an even simpler approach, Project Euler has used a pseudo random number generator:
S0 = seed
Sn+1 = Sn2 mod 50515093
If you want something close to 2^32, change the mod to 0xFFFFFFFB (the number 4,294,967,291 - the largest 32 bit prime)
The man page for rand has the following implementation from the POSIX.1-2001 specification:
static unsigned long next = 1;
/* RAND_MAX assumed to be 32767 */
int myrand(void) {
next = next * 1103515245 + 12345;
return((unsigned)(next/65536) % 32768);
}
void mysrand(unsigned seed) {
next = seed;
}
Look at the Art of Computer programming, volume 2 - it has a section dedicated to random number generation.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.