We can generate psedorandom numbers by generating a sequence of random-seeming numbers. This is what Python's `random.random()`

does. One way to generate pseudo-random numbers is to use a *linear congruential generator*: we repeatedly compute `x`

using

```
x = (a*x+b) % m
```

This will generate random seeming numbers:

In [1]:

```
a = 1664525
b = 1013904223
m = 2**32 - 1
x = 1
```

In [2]:

```
for i in range(10):
x = (a*x+b) % m
print(x)
```

If we want numbers in the range 0..1, we can divide by `m`

:

In [3]:

```
for i in range(10):
x = (a*x+b) % m
print(x/m)
```

In [6]:

```
a = 1664524
b = 0
m = 2**32
x = 1
```

In [7]:

```
for i in range(10):
x = (a*x+b) % m
print(x)
```

The seed determines where we start the sequence of pseudo-random numbers. Above, we started with x = 1.

If we want consistent output across runs, we'll set the seed to some set number.

If we want to get a different output every time, we can set the seed to time.time() -- the epoch time we get from the clock. (So you can use `random.seed(time.time())`

).

`myrandom`

, and we'll write it so that we can use it just like `random`

:

In [8]:

```
# myrandom.py
def myrandom():
global x
x = (a*x+b) % m
return x/m
def seed(s):
global x
x = (a*x+b) % m
def initialize():
global a, b, n, x
a = 1664525
b = 1013904223
m = 2**32 - 1
x = 1
initialize() # call initialize outside the main block so that the seed as well as the constants are set
```

We can now use this code in another file:

```
import myrandom
import time
if __name__ == '__main__':
# Will print the same sequence every time:
myrandom.seed(0)
print(myrandom.myrandom())
print(myrandom.myrandom())
print(myrandom.myrandom())
# The same sequence as above again
myrandom.seed(0)
print(myrandom.myrandom())
print(myrandom.myrandom())
print(myrandom.myrandom())
# A different sequence every run, since we are seeding using time.time()
myrandom.seed(int(time.time())
print(myrandom.myrandom())
print(myrandom.myrandom())
print(myrandom.myrandom())
```