CMWC (Complementary Multiply-with-carry) Matlab
To cite Mr. Marsaglia about generating more parameters for the CMWC 开发者_运维问答PRNG:
"Those wanting even more pairs r,a will need to find primes of the form p=ab^r+1 for which b=2^32-1 is a primitive root".
My question is in the method I should be using to do this. Especially with very large primes. This is what I've written in MATLAB:
isPrimitiveRoot = 0;
goodParameters = zeros(1,vectorSize);
nextFreeSpace = 1;
r = 1;
b = 2^32-1;
for a=0:2^32-1
isPrimitiveRoot = 0;
number = a*b^r+1;
if(isprime(number))
p = number;
phi_p = p - 1;
factors = factor(phi_p);
isPrimitiveRoot = 1;
for i=1:length(factors)
if(isprime(factors(i)))
if(mod(b^(phi_p/factors(i)),p)==1)
isPrimitiveRoot = 0;
end
end
end
end
if(isPrimitiveRoot)
goodParameters(nextFreeSpace) = a;
disp([nextFreeSpace a]);
nextFreeSpace = nextFreeSpace + 1;
end
end
I'm doing this because the steps to find good a
parameters for a certain r
lag are:
- Prove that
p = a*b^r+1
is prime - Prove that
b
is a primitive root ofp
. For that you need to evaluate the prime factors ofp-1
and verify thatb^((p-1)/p_i) =/= 1 (mod(p))
for allp_i
prime factors ofp-1
.
Now it's pretty obvious why the script doesn't work. I've chosen b = 2^32 -1
, and a lag r = 1
to keep it simple. But evaluating b^(phi_p/factors(i))
yields numbers simply too large (Inf
).
- What should I be doing instead?
- Should I be using another software?
- Is there another algorithm for verifying primitive roots?
Well one could always use my vpi toolbox in matlab. While I did not provide a tool to explicitly generate/test for a primitive root, vpi does have the ability to work with arbitrarily large integers, as well as a powermod function to do much of the work.
I will point out however, that a simple, brute force loop over 2^32 elements will take a while to accomplish here, regardless of the tool.
精彩评论