Fibonacci Linear Feedback Shift Register (Pseudo-Random Number Generator)

Dependents:   SDP_K1_Rand FibonacciLFSR_LED_Demo

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers FibonacciLFSR.h Source File

FibonacciLFSR.h

00001 #ifndef FIBONACCI_LFSR
00002 #define FIBONACCI_LFSR
00003 
00004 /**
00005     Library for Fibonacci Linear Feedback Shift Register pseudo-random number
00006     generator. The uniqueness of the Fibonacci LFSR is that all permutations
00007     (values) are explored except zero, ensuring at least one bit in the LFSR
00008     is always HIGH. 
00009     
00010     Applications: Random Number Generation, LED Faux Candles, LED lighting 
00011     effects, etc. 
00012     
00013     References: 
00014     https://en.wikipedia.org/wiki/Linear-feedback_shift_register
00015 */
00016 // TODO: Consider making this class static
00017 
00018 #include "mbed.h"
00019 
00020 class FibonacciLFSR{
00021     public:
00022         typedef enum SIZE{
00023             LFSR_2 = 2, LFSR_4 = 4, 
00024             LFSR_8 = 8, LFSR_16 = 16
00025         }LFSR_SIZE_t;
00026         /**
00027             Constructor of Fibonacci Linear Feedback Shift Register Objects.
00028             Preconditions: None
00029             @param size     Number of bits for LFSR 
00030         */   
00031         FibonacciLFSR(LFSR_SIZE_t size);
00032         
00033         /**
00034             Generate and return new random number
00035             @return    A new random value
00036         */
00037         uint16_t getRandom();
00038         
00039     private:
00040         LFSR_SIZE_t size;              // The number of bits representing the LFSR
00041         int lfsr_period;               // Period of LFSR register (number of random values before repeating)
00042         uint16_t lfsr;              
00043         uint16_t feedback_bit;      
00044 };
00045 #endif