Random Numbers with LFSR (Linear Feedback Shift Register) - Computerphile
**The Basics of Linear Feedback Shift Registers (LFSRs) and Their Applications**
In this article, we will explore the concept of Linear Feedback Shift Registers (LFSRs), which are a type of digital circuit used to generate pseudorandom binary sequences. An LFSR is a feedback shift register that uses a linear combination of its bits to produce a new bit output.
**Creating an LFSR in Python**
To create an LFSR, we can start with the basics of what we want to achieve. In this case, we want to generate a binary stream that appears random and unpredictable. We'll start by defining our initial state, which will be used as the starting point for our LFSR. For this example, we'll use a simple 3-bit LFSR with an initial state of one shifted 127 positions to the left, followed by a right and left shift.
```
state = [1 << 127] + [0, 1]
```
Next, we want to define our taps, which are the binary numbers that determine how each bit in the state is combined with its shifted predecessors. For this example, we'll use a simple tap of 0, 1, and 7.
```
taps = [0, 1, 2, 7]
```
Now, we can define our LFSR function, which will take the current state as input and produce the next bit output. We'll use a loop to iterate through each tap and perform the necessary operations.
```
def lfsr(state):
for i in taps:
# Combine the current state with its shifted predecessors
# using binary addition
new_state = [state[j] | (state[(j + i) % len(state)] << (len(state) - j - 1)) for j in range(len(state))]
# Shift the bits to the right
new_state = new_state[1:] + [new_state[0]]
return new_state[-1]
```
**Outputting the LFSR Stream**
Finally, we can use our LFSR function to generate a binary stream and output it to the console. We'll start with an empty string and append each new bit as we generate it.
```
stream = ''
while True:
state = [0] + lfsr(state)
stream += str(state[-1])
# Print the current state for reference
print('State:', ' '.join(str(x) for x in state))
```
This will produce a continuous binary stream that appears random and unpredictable. The period of this LFSR is 2^128 - 1, which means it will take an incredibly long time to repeat itself.
**Applications of LFSRs**
LFSRs have many applications in real-world systems, including:
* Random Number Generation: LFSRs are often used as a source of random numbers for simulations, modeling, and other applications.
* Stream Ciphers: LFSRs can be combined using various techniques to produce stream ciphers, which are used for encrypting data.
* Error Correction Codes: LFSRs can be used to generate error correction codes, such as the Hamming code.
**The Limitations of LFSRs**
While LFSRs are incredibly useful tools, there are some limitations to their use. For example:
* Non-Linearity: LFSRs are linear by design, which means they can be vulnerable to attacks based on non-linearity.
* Periodicity: The period of an LFSR is limited by the number of bits in its state, making it impractical for very long sequences.
Overall, LFSRs are powerful tools that have numerous applications in digital systems. By understanding their basics and limitations, we can harness their power to generate high-quality random numbers or build robust stream ciphers.