lfsr.h File Reference
#include <stdint.h>

Description

A linear-feedback shift register pseudo-random number generator.

Author
Scott Fazackerley

This code implements a simple random number generator that generates a subset of possible integral values (16 bit unsigned) such that there are no collisions within the cycle generated.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
1.Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.
2.Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
3.Neither the name of the copyright holder nor the names of its contributors
may be used to endorse or promote products derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

Definition in file lfsr.h.

Include dependency graph for lfsr.h:
This graph shows which files directly or indirectly include this file:

Classes

struct  lfsr
 An instance type for the random number generator. More...
 

Typedefs

typedef struct lfsr lfsr_t
 An instance type for the random number generator. More...
 

Functions

void lfsr_init_start_state (uint16_t seed, lfsr_t *instance)
 Initialize the seed for the random number generator. More...
 
uint16_t lfsr_get_next (lfsr_t *instance)
 Generate the next random number. More...
 
void lfsr_reset (lfsr_t *instance)
 Reset the random number generator to it's initial state. More...
 

Typedef Documentation

typedef struct lfsr lfsr_t

An instance type for the random number generator.

Function Documentation

uint16_t lfsr_get_next ( lfsr_t instance)

Generate the next random number.

Parameters
instanceA pointer to the random number generator object that we wish to use to generate the next random item.
Returns
The next random integer from the random number generator.

Definition at line 55 of file lfsr.c.

57  {
58  unsigned lsb = instance->lfsr_value & 1;/* Get LSB (i.e., the output bit). */
59 
60  instance->lfsr_value >>= 1; /* Shift register */
61 
62  if (lsb == 1) {
63  /* Only apply toggle mask if output bit is 1 */
64  instance->lfsr_value ^= 0xB400u;/* Apply toggle mask, value has 1 at bits corresponding
65  * to taps, 0 elsewhere. */
66  }
67 
68  return instance->lfsr_value;
69 }
uint16_t lfsr_value
Definition: lfsr.h:54
void lfsr_init_start_state ( uint16_t  seed,
lfsr_t instance 
)

Initialize the seed for the random number generator.

Parameters
seedThe initial start state represented as an integer for the random number generator.
instanceA pointer to the random number generator object to be initialized. This is caller allocated.

Definition at line 43 of file lfsr.c.

46  {
47  instance->start_state = seed;
48  instance->lfsr_value = seed;
49 }
uint16_t lfsr_value
Definition: lfsr.h:54
uint16_t start_state
Definition: lfsr.h:53
void lfsr_reset ( lfsr_t instance)

Reset the random number generator to it's initial state.

Parameters
instanceA pointer to the random number generator object to reset.

Definition at line 75 of file lfsr.c.

77  {
78  instance->lfsr_value = instance->start_state;
79 }
uint16_t lfsr_value
Definition: lfsr.h:54
uint16_t start_state
Definition: lfsr.h:53