CS 227                                                                                                                       W '05
On RNG test                                                                                                              RWN

Here's some Mathematica that might be relevant to Random Number Generation.

First of all, here's an attempt at a LCM:

a = 21 ; b = a - 1 ; c = 15 ; m = 100 ; f[x_] := Mod[a * x + c, m] x0 = 5 ; NestList[f, x0, 20]

{5, 20, 35, 50, 65, 80, 95, 10, 25, 40, 55, 70, 85, 0, 15, 30, 45, 60, 75, 90, 5}

Oops,  we see a period of 20. With m = 100, we should be able to get a period of 100.
What went wrong? The conditions of the maximum period theorem were violated. In particular consider

GCD[b, m]

20

So condition iii) is ok, i.e. 4 divides both b and m.

FactorInteger[b] FactorInteger[m]

{{2, 2}, {5, 1}}

{{2, 2}, {5, 2}}

Condtion ii) is ok too, the only prime divisors of both b and m are 2 and 5.

Now for condition i)

GCD[c, m]

5

Aha! c and m are not relatively prime, i.e. they have a common factor (other than 1).

The following Mathematica functions might also  be useful in seaching for larger and compatible a, c and m. (They're not hard to find.)

Prime[n]  gives the nth prime.
PrimeQ[n]  returns whether or not n is prime.
PrimePi[n]  gives the number of primes ≤ n.

Prime[1] Prime[5] Prime[100]

2

11

541

PrimeQ[541]

True

PrimeQ[11111111]

False

FactorInteger[11111111]

{{11, 1}, {73, 1}, {101, 1}, {137, 1}}

PrimePi[9] PrimePi[10] PrimePi[11]

4

4

5

And, for future reference, PowerMod[

PowerMod[13, 2, 3]

1

? PowerMod

PowerMod[a, b, n] gives a^b mod n. For negative b, PowerMod[a, b, n] gives modular inverses. More…

Mathematica functions used: FactorInteger, GCD, Mod, NestList, Prime, PrimePI,PrimeQ, PowerMod

If you find some others to be useful for applying the max period test, let me know.


Created by Mathematica  (January 27, 2005)