top | item 13597905

14 Character Random Number Generator in C

61 points| orangeduck | 9 years ago |theorangeduck.com

17 comments

order

unwind|9 years ago

Cool, and even cooler that the idea is due to Knuth.

I found this typo entertaining:

Technically it could be replaced by any other large prime number. The most important thing is that it must have few factors, and be large enough to distribute information into the higher value bits when the integer overflows.

Surely any prime number has "few factors", i.e. none other than itself and 1? I guess the "prime" in the first sentence is a typo, since the second sentence reads as if the first one didn't say "prime".

FabHK|9 years ago

My understanding was that for it to work, it must have few factors, and in particular no small ones, so easiest just to chose a large prime number.

quinnftw|9 years ago

I noticed that too, I think he may be referring to the fact that generating super large actual prime numbers is hard, so people use probabilistic algorithms to generate probably almost primes

FabHK|9 years ago

I'm glad that the author included a disclaimer: this is fine for generating pleasing pictures, but should not be used for anything serious (such as crypto, or even Monte Carlo simulations etc.)

eutectic|9 years ago

A 15 character 64 to 32-bit generator I devised which passes dieharder:

    (x+=x*x+9)>>32;

Someone|9 years ago

That can be made shorter:

  x+=x*x+9;
Are you sure you don't mean

  x+=((x*x+9)>>32);
(and does that need the outer parentheses?) I doubt the first passes dieharder, as (if my C isn't too rusty) it alternates between odd and even numbers.

jcahill84|9 years ago

This is really elegant. It's always fun to find something artsy in code. We often forget how important creativity is in software engineering.

joegosse|9 years ago

Thank you for the writeup. What was the tool used to generate the .GIF of the shell?