One way to generate pseudorandom generator is the Linear Congruential Generator . The generator is defined by the congruential relation where and are parameters of the generator and is called the seed.
Here is one way we could implement this:
1 2 3 4 5 |
|
However, linear congruential generators are not very secure, i.e. their outputs are fairly predictable.
Here are 8 consecutive outputs from a particular LCG:
1 |
|
What is the next output from the generator?
This section requires Javascript.
You are seeing this because something didn't load right. We suggest you, (a) try
refreshing the page, (b) enabling javascript if it is disabled on your browser and,
finally, (c)
loading the
non-javascript version of this page
. We're sorry about the hassle.
There is a nice trick to solve for m . Let Y n = X n − X n − 1 . Then Y n ≡ ( a X n − 1 + b ) − ( a X n − 2 + b ) ≡ a ( X n − 1 − X n − 2 ) ≡ a Y n − 1 ( m o d m ) . Now let Z n = Y n Y n + 2 − Y n + 1 2 . Then Z n is congruent to 0 mod m .
(Proof: each Y k is congruent to a Y k − 1 , so we can express everything in terms of Y n : Z n = Y n Y n + 2 − Y n + 1 2 ≡ Y n ( a 2 Y n ) − ( a Y n ) ( a Y n ) ≡ 0 ( m o d m ) . )
Since each Z n is a multiple of m , the idea is to generate enough values of Z n and take their gcd, and expect that the answer equals m . (With sufficiently many Z n , we expect there to be no larger common factor than the one that is guaranteed, namely m . )
Anyway, the gcd of Z 1 , … , Z 6 is m = 1 0 0 0 0 0 0 0 0 7 .
Given m , it is easy enough to solve for a and b ; just use a X 1 + b a X 2 + b ≡ X 2 ≡ X 3 to get two equations in the unknowns a , b , whose solutions are a ≡ X 1 − X 2 X 2 − X 3 , b ≡ X 2 − a X 1 . (These computations are all done mod m . When I did them, I cheated and used that m was prime, which let me compute 1 / d as d m − 2 ( m o d m ) . Without the cheat, I would have had to write an extended Euclidean algorithm function to compute the modular inverse of X 1 − X 2 .)
This gives a = 1 6 6 4 5 2 5 and b = 1 3 9 0 4 2 1 6 , whence the next term is 1 6 6 4 5 2 5 ⋅ 2 0 0 1 8 3 3 4 5 + 1 3 9 0 4 2 1 6 ≡ 1 9 3 9 0 7 8 7 1 ( m o d 1 0 0 0 0 0 0 0 0 7 ) .