14 Character Random Number Generator in C(theorangeduck.com) |
14 Character Random Number Generator in C(theorangeduck.com) |
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".
I haven't benchmarked it, but I think that should easily run in under a second on any phone sold in the last 10 years. If you do it a bit smarter and skip even divisors, I think it could run in under a second on 30 year old hardware (25k iterations, 200-ish cycles per loop (division was very expensive, back then) takes a 5MHz CPU)
(x+=x*x+9)>>32; 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.e.g:
uint64_t x = 0;
...
uint32_t y = (x+=x*x+9)>>32;At a generous one minute to prove primality of numbers in that range using trial division, and assuming you forget to bail out early when you find the first divisor, you still very likely will find a prime within an hour.
Even in the 70s, when computer time cost real money, that shouldn’t deter you.
(I just tried this on a Mac Mini, in Swift; finding 1000 primes took about a third of a second; 3 seconds if I forget to bail out early)
Edit: as further evidence that this isn't that large a prime: we knew 2^31-1 to be prime by trial division in 1772 (Euler somehow found time to do that)